0

I have release date in database as:

release_date
2010-12-02 00:00:00

and i have to get 50 rows from the data order by release date desc. Can anyone tell me how can i implement this in mysql sql query, some thing like

SELECT data.content,item.value
FROM contents 
 AS data 
JOIN items 
 AS item 
WHERE data.id=item.content_id 
ORDER BY data.release_date

please help, any idea will be highly appreciated.

EDIT

I am not asking how to limit it, but my problem is this query makes my script to run forever. Sorry for not clearing this before.

EDIT 2

contents
id | content | Comment | release_date

items
id | content_id | name | release_date

my both tables. Both have around 250000 rows

3
  • Have you tried your query? What is the problem with it? Commented Oct 20, 2011 at 7:56
  • MySQL is working with date perfectly. It should be OK to sort it with ORDER BY data.release_date DESC without converting it to UNIX Timestamp Commented Oct 20, 2011 at 7:57
  • yes i tried but this query made my script run forever. its execution doesnt stop. and i also dont see any data as output. Commented Oct 20, 2011 at 7:58

5 Answers 5

2

Add the DESC and LIMIT in your existing query as like below

SELECT data.content,item.value
FROM contents 
 AS data 
JOIN items 
 AS item 
WHERE data.id=item.content_id 
ORDER BY data.release_date DESC
LIMIT 50
Sign up to request clarification or add additional context in comments.

2 Comments

@Astha: Do you have indexes created on columns, especially on data.id and item.content_id?
@Astha: edit does not have much info, You have to post definition of tables. You can look up for the indexes using the query explain table_nane
1

same as you have but than:

ORDER BY data.release_date DESC LIMIT 50;

1 Comment

Could you include a raw database structure?
1

Try this

$sql = "select data.content, item.value 
FROM contents as data 
JOIN items as item ON data.id=item.content_id 
ORDER BY data.release_date DESC
LIMIT 50";

Note the DESC for ordering in descending order, remove this clause and it will set to order in ascending order

Comments

1

It seems like you are most of the way there already. You missed DESC and you should add a LIMIT clause:

ORDER BY data.release_date DESC
LIMIT 50

Comments

0

Would be something like:

select data.content,item.value
from contents as data
   join items as item ON data.id=item.content_id
ORDER BY data.release_date

It's better to use ON clausule to join tables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.