MySQL Query from 2 Tables?
January 7, 2008 9:07 AM
Subscribe
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?
I'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 'duh' answer, but I don't really get it.
So I'm registering people for our class reunion. Their data gets entered into the 'users' 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 'paypal' table. (An extra field in the paypal table lists their users.id ID.)
In the directory page that basically does a while() loop to display everyone's information with a basic SELECT * FROM 'users' query, I'd like to be able to also show which people have purchased tickets. The way I'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?
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 "blah". Thanks!
posted by gramcracker to computers & internet (9 comments total)
2 users marked this as a favorite
posted by 0xFCAF at 9:10 AM on January 7