Jump to content

Ordering & Limiting multiple table rows by date


masteroleary

Recommended Posts

My goal is to select rows from two tables 'shoot' & 'video' and have mysql order those tables by date and limit the number of rows returned. I want to list the latest 10 rows but am uncertain how many rows from each table will be the latest. I may have more recent shoots than videos or the opposite. How can I use a single ORDER BY and LIMIT statement for both selections?

 

This is the code I used last which may or may not be anywhere near the direction I need to go.

SELECT id, name, description, directory, date
FROM shoot
LIMIT 3 
UNION (

SELECT id, name, description, filename, date
FROM video
)
ORDER BY date
LIMIT 0 , 30

Link to comment
Share on other sites

SELECT id, name, description, directory, `date`
FROM shoot
LIMIT 3 
UNION (
SELECT id, name, description, filename, `date`
FROM video
)
ORDER BY `date`
LIMIT 0,30

 

The limit you should not have any spaces. If you are using MySQL make sure you are using a version that supports subquerys. The date column can be considered a "special" word, using the backticks (`) to enclose it may also help.

 

If that does not work do OR DIE(mysql_error()) and report back the error or show some code.

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.