ianh Posted January 20, 2010 Share Posted January 20, 2010 Hi, Bascially I want to select 2 records but with different criteria How would I get the following 2 select queries into 1? SELECT title, abstract, body FROM Articles WHERE category_id = 1 AND id = 3015; SELECT title, abstract, body FROM Articles WHERE category_id = 1 AND id > 3015 ORDER BY order_num ASC LIMIT 1; Help much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/189164-2-mysql-queries-into-1/ Share on other sites More sharing options...
cyberRobot Posted January 20, 2010 Share Posted January 20, 2010 Have you tried using UNION? (SELECT title, abstract, body FROM Articles WHERE category_id = 1 AND id = 3015) UNION (SELECT title, abstract, body FROM Articles WHERE category_id = 1 AND id > 3015 ORDER BY order_num ASC LIMIT 1) Quote Link to comment https://forums.phpfreaks.com/topic/189164-2-mysql-queries-into-1/#findComment-998697 Share on other sites More sharing options...
ignace Posted January 20, 2010 Share Posted January 20, 2010 or SELECT title, abstract, body FROM Articles WHERE category_id = 1 AND id >= 3015 ORDER BY id ASC, order_num ASC LIMIT 1; Quote Link to comment https://forums.phpfreaks.com/topic/189164-2-mysql-queries-into-1/#findComment-998710 Share on other sites More sharing options...
ianh Posted January 20, 2010 Author Share Posted January 20, 2010 Thanks for the replies. However I believe your queries are just returning 1 record. I need it to return 2 records, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/189164-2-mysql-queries-into-1/#findComment-998765 Share on other sites More sharing options...
cyberRobot Posted January 20, 2010 Share Posted January 20, 2010 Did you try the UNION example? It should give you both records. Also, is there a reason why you need it to be one statment? You could just process both statements and work with the results afterwards. Quote Link to comment https://forums.phpfreaks.com/topic/189164-2-mysql-queries-into-1/#findComment-998785 Share on other sites More sharing options...
ianh Posted January 20, 2010 Author Share Posted January 20, 2010 Did you try the UNION example? It should give you both records. Also, is there a reason why you need it to be one statment? You could just process both statements and work with the results afterwards. I stand corrected It was my PHP code displaying the result incorrectly. cyberRobot, I ran your union statement in phpmyadmin and it works fine. Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/189164-2-mysql-queries-into-1/#findComment-998804 Share on other sites More sharing options...
ignace Posted January 20, 2010 Share Posted January 20, 2010 or SELECT title, abstract, body FROM Articles WHERE category_id = 1 AND id >= 3015 ORDER BY id ASC, order_num ASC LIMIT 1; I hadn't noticed the LIMIT 1, this should do it: SELECT title, abstract, body FROM Articles WHERE category_id = 1 AND id >= 3015 ORDER BY id ASC, order_num ASC; Try this one because UNION is IMO overkill Quote Link to comment https://forums.phpfreaks.com/topic/189164-2-mysql-queries-into-1/#findComment-998816 Share on other sites More sharing options...
cyberRobot Posted January 20, 2010 Share Posted January 20, 2010 I hadn't noticed the LIMIT 1, this should do it: SELECT title, abstract, body FROM Articles WHERE category_id = 1 AND id >= 3015 ORDER BY id ASC, order_num ASC; Try this one because UNION is IMO overkill I would imagine that would give more than the two records the OP was looking for. Note that I agree that UNION is overkill. Using two seperate queries would probably be the way to go. Then process the results afterwards. Quote Link to comment https://forums.phpfreaks.com/topic/189164-2-mysql-queries-into-1/#findComment-998835 Share on other sites More sharing options...
ignace Posted January 20, 2010 Share Posted January 20, 2010 Well the OP hasn't clearly stated for what he is using it but in that case a LIMIT 2 would suffice. The first record would be the where id = 3015 because of ORDER BY id ASC, order_num ASC Quote Link to comment https://forums.phpfreaks.com/topic/189164-2-mysql-queries-into-1/#findComment-998950 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.