| | |
Query multiple tables?
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2005
Posts: 1
Reputation:
Solved Threads: 0
I'm having trouble figuring out how to query multiple tables.
(Note: I've greatly simplified the tables for clarity purposes)
I have 2 tables: Invites and Purchases
The invites table has the following fields: email & invitee
The purchases table has the following fields: useremail & product
Invites tables
+------------------+----------------------+
| email | invitee |
+------------------+----------------------+
| joe@joe.com | sam@sam.com |
| joe@joe.com | max@max.com |
| joe@joe.com | ken@ken.com |
+------------------+----------------------+
Purchases table
+------------------+----------------------+
| useremail | product |
+------------------+----------------------+
| joe@joe.com | gum |
| sam@sam.com | cheese |
| ken@ken.com | cheese |
+------------------+----------------------+
It's a simple referral program where if joe@joe.com sends an invitation to sam@sam.com, joe gets credit if sam buys something.
But, what I want to do is this:
I want to find all the useremail from the purchases table, and all the useremails in the purchases table that stem from the invites sent, and then display one of these
emails randomly.
Basically, I want to find a way to combine the following statements:
1) SELECT useremail FROM purchases;
2) SELECT email FROM invites WHERE purchases.useremail=invites.invitee
3) ORDER BY RAND() LIMIT 1;
I've looked at JOIN and UNION, but neither are making much sense to me, and everything I try generates an error message.
Is this possible? Can anyone help out?
(Note: I've greatly simplified the tables for clarity purposes)
I have 2 tables: Invites and Purchases
The invites table has the following fields: email & invitee
The purchases table has the following fields: useremail & product
Invites tables
+------------------+----------------------+
| email | invitee |
+------------------+----------------------+
| joe@joe.com | sam@sam.com |
| joe@joe.com | max@max.com |
| joe@joe.com | ken@ken.com |
+------------------+----------------------+
Purchases table
+------------------+----------------------+
| useremail | product |
+------------------+----------------------+
| joe@joe.com | gum |
| sam@sam.com | cheese |
| ken@ken.com | cheese |
+------------------+----------------------+
It's a simple referral program where if joe@joe.com sends an invitation to sam@sam.com, joe gets credit if sam buys something.
But, what I want to do is this:
I want to find all the useremail from the purchases table, and all the useremails in the purchases table that stem from the invites sent, and then display one of these
emails randomly.
Basically, I want to find a way to combine the following statements:
1) SELECT useremail FROM purchases;
2) SELECT email FROM invites WHERE purchases.useremail=invites.invitee
3) ORDER BY RAND() LIMIT 1;
I've looked at JOIN and UNION, but neither are making much sense to me, and everything I try generates an error message.
Is this possible? Can anyone help out?
Try this:
http://www.google.de/search?hl=de&q=...le-Suche&meta=
Michael
MySQL Syntax (Toggle Plain Text)
SELECT p.useremail useremail, i.email email FROM Purchases p, Invites i WHERE p.useremail=i.invitee ORDER BY RAND() LIMIT 1;
http://www.google.de/search?hl=de&q=...le-Suche&meta=
Michael
![]() |
Similar Threads
- Query multiple tables with duplicate data (MySQL)
- Searching for a record in multiple tables (VB.NET)
- Multiple Tables in a Database (Visual Basic 4 / 5 / 6)
- Delete from multiple tables (MySQL)
Other Threads in the MySQL Forum
- Previous Thread: Remotly Hosted MySQL Database
- Next Thread: hav a problem with this query
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database design developer development distinct drupal dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop legal license licensing linux maintenance mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization





