emehrkay Posted May 3, 2006 Share Posted May 3, 2006 i want to join two tables - names and class_enrollment. the common column is person_idi want to have a simple list, first_name, last_name etc, but if they are enrolled in that class i want the enrollment to return 1so i was thinking that i could do something like thisSELECTfirst_name, if(SELECT person_id FROM class_enrollment ce WHERE ce.person_id = n.person_id AND ce.class_id = 72, 1, 0) AS enrolledFROMnames nthat is givin me the error"You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT person_id FROM class_enrollment ce WHERE ce.person_id = n.person_id, 1, 0' at line 2"and im not sure what i am doing wrongany help, thanks Quote Link to comment Share on other sites More sharing options...
ober Posted May 3, 2006 Share Posted May 3, 2006 You're looking for a subquery (and your version of MySQL has to support it so you can do it).[code]SELECT first_name AS enrolledFROM names nWHERE person_id IN (SELECT person_id FROM class_enrollment ce WHERE ce.person_id = n.person_id AND ce.class_id = 72, 1, 0)[/code]If your version of MySQL doesn't support subqueries, you can always run the subquery as a seperate query first and use the IN WHERE X IN clause with the ids grabbed from the first query. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted May 3, 2006 Author Share Posted May 3, 2006 thanks ober, but i figured it out. i just needed prens around the subquery and its working as expected Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.