Jump to content

2 MySQL queries into 1


ianh

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/189164-2-mysql-queries-into-1/
Share on other sites

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!

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.