Page is a not externally linkable
- Code, Content, and Presentation
-- Databases
---- Combing multiple statements


ahmed24 - 3:59 pm on Dec 29, 2012 (gmt 0)


I have 3 different tables: user_reg, user_contacts and contacts.

I have the following SQL statement that returns all the user_id's that I am interested in using:


SELECT user_id FROM user_reg WHERE year='2012' AND end_date IS NULL


I then need to use the returned user_id's to query the user_contacts table to find all the contact_id's where the user_id matches the results of the previous sql statement. e.g:


SELECT contact_id FROM user_contacts WHERE user_id='From Above User_ID'


Now that i have the contact_id, i need to select email, phone and mobile data from the contacts table where the id matches the previous contact_id. e.g:


SELECT email, phone, mobile FROM contacts WHERE id='From Above Contact_ID'


Without doing this within multiple while or foreach loops, is there any way to combine these three statements into one so that the results are returned as one?

Thanks


Thread source:: http://www.webmasterworld.com/databases_sql_mysql/4531887.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com