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
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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.