简体   繁体   English

MySQL日期时间戳SQL查询

[英]Mysql Date to timestamp SQL Query

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. 而且我必须在发布日期desc之前从数据顺序中获取50行。 Can anyone tell me how can i implement this in mysql sql query, some thing like 谁能告诉我如何在mysql sql查询中实现此功能,例如

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 编辑2

contents
id | content | Comment | release_date

items
id | content_id | name | release_date

my both tables. 我的两张桌子。 Both have around 250000 rows 两者都有大约250000行

Add the DESC and LIMIT in your existing query as like below 如下所示,在现有查询中添加DESCLIMIT

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

与您相同,但比:

ORDER BY data.release_date DESC LIMIT 50;

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 注意DESC按降序排列,删除此子句,它将设置为按升序排列

It seems like you are most of the way there already. 看来您已经完成大部分工作了。 You missed DESC and you should add a LIMIT clause: 您错过了DESC,应该添加LIMIT子句:

ORDER BY data.release_date DESC
LIMIT 50

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. 最好使用ON子句来连接表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM