hostfreak Posted August 1, 2007 Share Posted August 1, 2007 I was wondering how I would combine queries that are stored in an array? The reason is to be able to order the multiple queries from the same "ORDER BY" which will be obtained from an url parameter. Also, the reasons the queries are in an array is, because they use the "WHERE" clause to SELECT "WHERE year = '$year'", and there could be several years (the years are obtained from $_POST) and I don't think there is a way to do something like: WHERE year = '$year[0],$year[1]'. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/ Share on other sites More sharing options...
Barand Posted August 1, 2007 Share Posted August 1, 2007 .... WHERE year IN ($year[0], $year[1]) or, easier, $yrs = join (',', $year); .... WHERE year IN ($yrs) Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313152 Share on other sites More sharing options...
hostfreak Posted August 1, 2007 Author Share Posted August 1, 2007 I won't always be sure of the amount of years, so in this case just run it through a foreach loop and it will still work, correct? Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313156 Share on other sites More sharing options...
Barand Posted August 1, 2007 Share Posted August 1, 2007 see my edit to my post Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313159 Share on other sites More sharing options...
hostfreak Posted August 1, 2007 Author Share Posted August 1, 2007 Ah, once again Barand, thanks. Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313161 Share on other sites More sharing options...
Barand Posted August 1, 2007 Share Posted August 1, 2007 BTW, in cases where this technique can't be used, the answer to your original question is SELECT a,b,c FROM table WHERE something UNION SELECT a,b,c FROM table WHERE something_else UNION SELECT ..... etc Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313167 Share on other sites More sharing options...
hostfreak Posted August 1, 2007 Author Share Posted August 1, 2007 Got rid of the arrays, ran the modified query, no result. Could it be due to me doing this: YEAR(time_stamp) IN ($yrs) I am actually comparing the years against the year found from the field 'time_stamp' (0000-00-00 00:00:00) with the mysql YEAR() function. Not sure if that matters? Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313176 Share on other sites More sharing options...
Barand Posted August 1, 2007 Share Posted August 1, 2007 What column type is "time_stamp" ? Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313179 Share on other sites More sharing options...
hostfreak Posted August 1, 2007 Author Share Posted August 1, 2007 BTW, in cases where this technique can't be used, the answer to your original question is SELECT a,b,c FROM table WHERE something UNION SELECT a,b,c FROM table WHERE something_else UNION SELECT ..... etc I had read about UNION and wanted to try it in this situation, but wasn't sure how to implement it where the queries were stored in an array then looped through as well as the results (as this is, or was if the other code works). Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313183 Share on other sites More sharing options...
hostfreak Posted August 1, 2007 Author Share Posted August 1, 2007 time_stamp is a "datetime" column type. Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313185 Share on other sites More sharing options...
Barand Posted August 1, 2007 Share Posted August 1, 2007 Something like WHERE YEAR(time_stamp) IN (2005, 2006, 2007) should work OK in that case. If you echo the querystring that you are executing, what do you get? Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313192 Share on other sites More sharing options...
hostfreak Posted August 1, 2007 Author Share Posted August 1, 2007 Echoing the querystring yields: SELECT equipment_number, plant, id, YEAR(time_stamp) as year, MONTHNAME(time_stamp) as month FROM work_orders_one WHERE equipment_number = '2' AND YEAR(time_stamp) IN (2007,2010) ORDER BY equipment_number ASC Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313197 Share on other sites More sharing options...
hostfreak Posted August 1, 2007 Author Share Posted August 1, 2007 Opps, I had a problem in my while loop. Your code works perfectly, sorry and thanks. Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313201 Share on other sites More sharing options...
hostfreak Posted August 1, 2007 Author Share Posted August 1, 2007 Also, if you have time to answer, for my own knowledge, how would one go about using UNION if the queries are contained in an array? Link to comment https://forums.phpfreaks.com/topic/62907-solved-combine-queries/#findComment-313202 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.