Jaynesh Posted July 11, 2011 Share Posted July 11, 2011 Hello How would I do a union select if each query had a different amount of rows? I tried adding NULL but that does not seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/241720-union-select-different-amount-of-rows/ Share on other sites More sharing options...
Kustom_Vegas Posted July 11, 2011 Share Posted July 11, 2011 you will want to use a join.. http://dev.mysql.com/doc/refman/5.0/en/join.html Quote Link to comment https://forums.phpfreaks.com/topic/241720-union-select-different-amount-of-rows/#findComment-1241466 Share on other sites More sharing options...
Jaynesh Posted July 11, 2011 Author Share Posted July 11, 2011 Hi, I'm already using a join on each query. Will it still work? Can I add an OR to merge both queries? For e.g SELECT user.id, post.id FROM user LEFT JOIN ON user.id = post.id OR SELECT vote.id, vote.up, vote.down FROM vote Quote Link to comment https://forums.phpfreaks.com/topic/241720-union-select-different-amount-of-rows/#findComment-1241471 Share on other sites More sharing options...
Kustom_Vegas Posted July 11, 2011 Share Posted July 11, 2011 just list the table references like so SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c) //taken from dev.mysql Quote Link to comment https://forums.phpfreaks.com/topic/241720-union-select-different-amount-of-rows/#findComment-1241473 Share on other sites More sharing options...
Jaynesh Posted July 11, 2011 Author Share Posted July 11, 2011 Hello Ok, i've done that but it isnt working. SELECT DISTINCT dbPosts.post FROM dbPosts LEFT JOIN ( dbUsers, dbFriends ) ON ( dbPosts.username_id = dbUsers.id AND (dbPosts.username_id = dbFriends.user_id) OR (dbPosts.username_id = dbFriends.friend_id)) LEFT JOIN (dbUsers, dbPosts_share, dbFriends) ON ( dbPosts.username_id = dbUsers.id AND (dbPosts.post_id = dbPosts_share.post_id) OR (dbPosts.username_id = dbPosts_share.user_id) AND (dbPosts_share.user_id = dbFriends.friend_id) OR (dbPosts_share.user_id = dbFriends.user_id)) Quote Link to comment https://forums.phpfreaks.com/topic/241720-union-select-different-amount-of-rows/#findComment-1241480 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.