rockinaway Posted December 29, 2011 Share Posted December 29, 2011 Is there any way that i can combine the results.from multiple queries? I can't use left.join as im.reliant on results.. So is there any way? Each set of query results have a time column for each row.. and basically after combining the results in a php array or something i want to order all the results cumulatively by time.. any way? Quote Link to comment https://forums.phpfreaks.com/topic/253988-combine-results-of-several-queries/ Share on other sites More sharing options...
trq Posted December 29, 2011 Share Posted December 29, 2011 UNION Quote Link to comment https://forums.phpfreaks.com/topic/253988-combine-results-of-several-queries/#findComment-1302037 Share on other sites More sharing options...
mikosiko Posted December 29, 2011 Share Posted December 29, 2011 show your tables and queries for better answer... UNION can be used if all the rules are followed. Quote Link to comment https://forums.phpfreaks.com/topic/253988-combine-results-of-several-queries/#findComment-1302039 Share on other sites More sharing options...
rockinaway Posted December 29, 2011 Author Share Posted December 29, 2011 Okay thanks, I'll try my best to lay out the situation (syntax won't be correct as I'm doing this very quickly before work). My idea has come from the social network stream/feed etc where you get information about yourself and your friend aggregated. However, in my case I'm using it for a project regarding companies. So My first query is to select all the companies that are friends with the user (user_id): SELECT friend_id FROM companies WHERE id = user_id Then I want to get all the posts that the user makes and load these: SELECT content, time FROM posts WHERE userid = user_id Using the first equation, I want to get all the friend_id posts as well SELECT content, time FROM posts WHERE userid = friend_id Also, in and amongst this, I'm going to try and get mutual friend posts as well but I need to think that one through even more. From the two last queries you can see that there is a time value. Basically, I want the results combined and then ordered by their times from this. Hope this helps a little. Quote Link to comment https://forums.phpfreaks.com/topic/253988-combine-results-of-several-queries/#findComment-1302135 Share on other sites More sharing options...
mikosiko Posted December 29, 2011 Share Posted December 29, 2011 and for what reason you can't do the 3 queries in just one sentence?... there is a clear relationship among them.. you could get all the data that you need with 1 query and process/display it at you wish. if that is not the route that you want to follow (??) then yes.. you can use UNION to mix data from queries 2 and 3 Quote Link to comment https://forums.phpfreaks.com/topic/253988-combine-results-of-several-queries/#findComment-1302181 Share on other sites More sharing options...
rockinaway Posted December 29, 2011 Author Share Posted December 29, 2011 Well for some reason I think it won't work in one query (there is a little more to be added to the queries). If I do several queries with UNION, and each query has a time result, can I order the results of ALL the queries by time? Quote Link to comment https://forums.phpfreaks.com/topic/253988-combine-results-of-several-queries/#findComment-1302361 Share on other sites More sharing options...
fenway Posted December 30, 2011 Share Posted December 30, 2011 If I do several queries with UNION, and each query has a time result, can I order the results of ALL the queries by time? Yes, you can. Quote Link to comment https://forums.phpfreaks.com/topic/253988-combine-results-of-several-queries/#findComment-1302619 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.