<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

      <title>Comments on: MySQL Query from 2 Tables?</title>
      <link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables/</link>
      <description>Comments on Ask MetaFilter post MySQL Query from 2 Tables?</description>
	  	  <pubDate>Mon, 07 Jan 2008 09:10:16 -0800</pubDate>
      <lastBuildDate>Mon, 07 Jan 2008 09:10:16 -0800</lastBuildDate>
      <language>en-us</language>
	  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
	  <ttl>60</ttl>

<item>
  	<title>Question: MySQL Query from 2 Tables?</title>
  	<link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables</link>	
  	<description>How do I query data from one MySQL table, but I also need to get a single piece of data from a second table too? Should I be doing a JOIN, or a separate query? &lt;br /&gt;&lt;br /&gt; I&apos;m a self-taught, on-the-fly, make-it-work PHP/MySQL coder. Do little easy side projects, with basic MySQL queries, so this might be a &apos;duh&apos; answer, but I don&apos;t really get it.&lt;br&gt;
&lt;br&gt;
So I&apos;m registering people for our class reunion. Their data gets entered into the &apos;users&apos; table. They can buy tickets online through Paypal, which is linked to their users.id ID, but their Paypal purchase info is stored in the &apos;paypal&apos; table. (An extra field in the paypal table lists their users.id ID.)&lt;br&gt;
&lt;br&gt;
In the directory page that basically does a while() loop to display everyone&apos;s information with a basic SELECT * FROM &apos;users&apos; query, I&apos;d like to be able to also show which people have purchased tickets. The way I&apos;m doing it now is having the Paypal script ALSO write to the users table to make users.tickets = 1 where id=X, which works fine, but there must be a more elegant way to do this from just one SELECT query involving both tables, right? &lt;br&gt;
&lt;br&gt;
I tried something like SELECT users.*, paypal.purchaseduserid FROM users, paypal ,  but that was screwing up the loop. I basically need to display all the data from the user field, but if the paypal table also has the purchaseduserid in it that matches the current user.id, echo &quot;blah&quot;. Thanks!</description>
  	<guid isPermaLink="false">post:ask.metafilter.com,2008:site.80375</guid>
  	<pubDate>Mon, 07 Jan 2008 09:07:58 -0800</pubDate>
  	<dc:creator>gramcracker</dc:creator>
	
	<category>mysql</category>
	
	<category>php</category>
	
	<category>database</category>
	
	<category>databasequery</category>
	
</item>
<item>
  	<title>By: 0xFCAF</title>
  	<link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables#1192118</link>	
  	<description>This is the exact situation joins were made for. I can&apos;t help you with MySQL&apos;s join syntax, but their docs should explain it.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.80375-1192118</guid>
  	<pubDate>Mon, 07 Jan 2008 09:10:16 -0800</pubDate>
  	<dc:creator>0xFCAF</dc:creator>
</item>
<item>
  	<title>By: bitdamaged</title>
  	<link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables#1192134</link>	
  	<description>so you really want a count&lt;br&gt;
&lt;br&gt;
SELECT users.*, count(purchaseuserid) as purchases FROM users&lt;br&gt;
LEFT JOIN paypal on users.id = paypal.purchaseduserid&lt;br&gt;
&lt;br&gt;
The left join syntax should give you all the records from users whether or not they have a purchase.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.80375-1192134</guid>
  	<pubDate>Mon, 07 Jan 2008 09:19:18 -0800</pubDate>
  	<dc:creator>bitdamaged</dc:creator>
</item>
<item>
  	<title>By: mkb</title>
  	<link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables#1192135</link>	
  	<description>Your two-table query is missing a join condition, and will give you back a resultset containing every possible combination of rows from users and rows from paypal. You need to add a WHERE users.id = paypal.id to limit the rows to data that actually match.&lt;br&gt;
&lt;br&gt;
If not every user in users has an entry in paypal, then you will need an outer join. In MySQL, that means you do SELECT users.*, paypal.purchaseduserid FROM users LEFT OUTER JOIN paypal ON users.id = paypal.id.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.80375-1192135</guid>
  	<pubDate>Mon, 07 Jan 2008 09:19:49 -0800</pubDate>
  	<dc:creator>mkb</dc:creator>
</item>
<item>
  	<title>By: flyingcowofdoom</title>
  	<link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables#1192136</link>	
  	<description>If there is a field in the paypal table called users_id which links that paypal record to a users record, the syntax is:&lt;br&gt;
&lt;br&gt;
SELECT users.*, paypal.purchaseuserid FROM users, paypal WHERE users.id = paypal.users_id;&lt;br&gt;
&lt;br&gt;
To limit to people that have bought tickets, its:&lt;br&gt;
&lt;br&gt;
SELECT users.*, paypal.purchaseuserid FROM users,paypal WHERE users.id = paypal.users_id AND users.tickets = 1;&lt;br&gt;
&lt;br&gt;
FYI, it is customary for table names to be singular.&lt;br&gt;
&lt;br&gt;
--FCOD</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.80375-1192136</guid>
  	<pubDate>Mon, 07 Jan 2008 09:19:52 -0800</pubDate>
  	<dc:creator>flyingcowofdoom</dc:creator>
</item>
<item>
  	<title>By: bitdamaged</title>
  	<link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables#1192137</link>	
  	<description>I think this might work too&lt;br&gt;
&lt;br&gt;
Select users.*, count(paypal.*) where paypal.purchaseusreid = users.id</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.80375-1192137</guid>
  	<pubDate>Mon, 07 Jan 2008 09:20:37 -0800</pubDate>
  	<dc:creator>bitdamaged</dc:creator>
</item>
<item>
  	<title>By: mkb</title>
  	<link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables#1192138</link>	
  	<description>&lt;a href=&quot;http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables#1192134&quot;&gt;bitdamaged&lt;/a&gt; left out something. That query needs a &apos;GROUP BY users.id&apos; a the end.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.80375-1192138</guid>
  	<pubDate>Mon, 07 Jan 2008 09:20:55 -0800</pubDate>
  	<dc:creator>mkb</dc:creator>
</item>
<item>
  	<title>By: Khalad</title>
  	<link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables#1192140</link>	
  	<description>You want to do a LEFT JOIN so that all user records are returned, regardless of whether there&apos;s a matching paypal record or not. Users with no paypal record will have the paypal columns set to NULL.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.80375-1192140</guid>
  	<pubDate>Mon, 07 Jan 2008 09:22:07 -0800</pubDate>
  	<dc:creator>Khalad</dc:creator>
</item>
<item>
  	<title>By: cschneid</title>
  	<link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables#1192149</link>	
  	<description>SELECT * FROM users&lt;br&gt;
LEFT JOIN paypal ON users.id = paypal.user_id&lt;br&gt;
&lt;br&gt;
The resulting table will have all the columns from both users and paypal.  If the user doesn&apos;t have any paypal transactions, the paypal column will be all NULLs.  If a given user has two paypal transactions, there will be 2 rows for that user, with the user columns being the same, and the paypal columns showing the two different transactions.&lt;br&gt;
&lt;br&gt;
Using a group by and a count, you could probably reduce that down, but that&apos;s over my head without the data in front of me to play with.&lt;br&gt;
&lt;br&gt;
So the loop will look like:&lt;br&gt;
&lt;code&gt;&lt;pre&gt;for (element in sql_result) {&lt;br&gt;
    print &amp;quot;user info&amp;quot;&lt;br&gt;
    if (not_null paypal_column) then print &amp;quot;has tickets&amp;quot;&lt;br&gt;
}&lt;br&gt;
&lt;/pre&gt;&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
This doesn&apos;t handle a user who has two separate paypal transactions, which again can be solved via a group by.  Feel free to email me with more details so I can figure out to handle that.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.80375-1192149</guid>
  	<pubDate>Mon, 07 Jan 2008 09:32:33 -0800</pubDate>
  	<dc:creator>cschneid</dc:creator>
</item>
<item>
  	<title>By: gramcracker</title>
  	<link>http://ask.metafilter.com/80375/MySQL-Query-from-2-Tables#1192170</link>	
  	<description>Man, these are all best answers. Thank you guys so much, and so quick! I thought JOIN had something to do with it, but didn&apos;t know enough about where to begin. I heart Ask Mefi.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.80375-1192170</guid>
  	<pubDate>Mon, 07 Jan 2008 09:43:13 -0800</pubDate>
  	<dc:creator>gramcracker</dc:creator>
</item>

    </channel>
</rss>
