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. Quote Link to comment 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) Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 1, 2007 Share Posted August 1, 2007 see my edit to my post Quote Link to comment Share on other sites More sharing options...
hostfreak Posted August 1, 2007 Author Share Posted August 1, 2007 Ah, once again Barand, thanks. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 1, 2007 Share Posted August 1, 2007 What column type is "time_stamp" ? Quote Link to comment 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). Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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? 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.