Techmate Posted August 24, 2012 Share Posted August 24, 2012 The problem that I am having is rather simple it seems but I am a little stumped I have been working on it for 2 days now :-\. As you see below it querys 217 two times and I just need it to query all the results only once. Any idea why??? SELECT id id, cast.booked booked FROM images, cast WHERE id = cast.memberid AND posterid = '51' UNION SELECT memberid id, images.booked booked FROM cast,images WHERE posterid='51' AND null !='' ORDER BY id ASC id | booked --------------- 217 217 yes 218 219 yes Link to comment https://forums.phpfreaks.com/topic/267533-help-mysql-union-not-querying-correctly/ Share on other sites More sharing options...
Christian F. Posted August 24, 2012 Share Posted August 24, 2012 What you want to use is a JOIN, not a UNION statement. Link to comment https://forums.phpfreaks.com/topic/267533-help-mysql-union-not-querying-correctly/#findComment-1372174 Share on other sites More sharing options...
Techmate Posted August 24, 2012 Author Share Posted August 24, 2012 I have tried that and are getting the same results. SELECT distinct memberid, id, cast.booked FROM images, cast WHERE memberid = images.id AND posterid = '51' Link to comment https://forums.phpfreaks.com/topic/267533-help-mysql-union-not-querying-correctly/#findComment-1372181 Share on other sites More sharing options...
Techmate Posted August 24, 2012 Author Share Posted August 24, 2012 What you want to use is a JOIN, not a UNION statement. Omg I started with a join query then moved on to UNION and you were right Join worked all I had to do is GROUP BY end LOL. THANKS! Link to comment https://forums.phpfreaks.com/topic/267533-help-mysql-union-not-querying-correctly/#findComment-1372186 Share on other sites More sharing options...
Christian F. Posted August 24, 2012 Share Posted August 24, 2012 You're welcome, and I'm glad I could help. Though, please use explicit JOIN statements, not implicit ones like the above. Doing it the right way will make the query a lot easier to read, and thus maintain. Saving you a lot of future headaches. Link to comment https://forums.phpfreaks.com/topic/267533-help-mysql-union-not-querying-correctly/#findComment-1372201 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.