MySQL SQL - ORDER BY 'datetime' DESC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24567274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:41:53  来源:igfitidea点击:

SQL - ORDER BY 'datetime' DESC

mysqlsqldatetime

提问by Super Cat

I have several values stored in my database as the DATETIMEdatatype (YYYY-MM-DD HH:MM:SS), and I've been trying to get them in a descending order - Greatest to least (In the case of dates - Newest to oldest), though, oddly enough, it seems to ignore the existence of the DESCoperator entirely.

我在我的数据库中存储了几个值作为DATETIME数据类型(YYYY-MM-DD HH:MM:SS),并且我一直在尝试按降序获取它们 - 最大到最少(在日期的情况下 - 最新到最古老),不过,奇怪的是,它似乎完全忽略了DESC运算符的存在。

The SQL query (Abbreviated):

SQL 查询(缩写):

SELECT post_datetime FROM post WHERE type=`published` ORDER BY post_datetime DESC LIMIT 3

And from that, they print in this order:

然后,他们按以下顺序打印:

2014-04-30 11:55:11
2014-07-01 12:25:40
2014-07-02 12:28:03

(Those happen to be the "oldest" date entries in the database too)

(那些恰好也是数据库中“最旧”的日期条目)

Solution? I'll note that using DESC on other things (Such as normal numbers) doesn't work either. I've checked my syntax, tried quotes, no quotes, double quotes & such.

解决方案?我会注意到在其他事物(例如普通数字)上使用 DESC 也不起作用。我检查了我的语法,试过引号,没有引号,双引号等。

(Note- I won't be able to answer any further questions for several hours, though I'll do my best to respond once I return)

注意- 我将在几个小时内无法回答任何其他问题,但我会尽我所能在我回来后做出回应)



(6/11/17)

(6/11/17)

Edit: To clarify, for future readers, the syntax typo was not related to the issue and was an error on my part when typing an example. I had been using a homemade query builder for so long that I had forgotten the proper syntax at the time. The issue lied within my program's logic—not the query. The above example has been edited and iscorrect solution for anyone looking to perform the mentioned task. I'm sorry this has been viewed over 50k times... but now you know.

编辑:为了将来的读者澄清,语法错字与问题无关,是我在键入示例时的错误。我一直在使用自制的查询构建器,以至于我当时忘记了正确的语法。问题在于我的程序逻辑——而不是查询。上面的示例已经过编辑,对于希望执行上述任务的任何人来说都是正确的解决方案。很抱歉这已经被观看了超过 5 万次……但现在你知道了。

回答by Uriil

  1. use single quotes for strings
  2. do NOT put single quotes around table names(use ` instead)
  3. do NOT put single quotes around numbers (you can, but it's harder to read)
  4. do NOT put ANDbetween ORDER BYand LIMIT
  5. do NOT put =between ORDER BY, LIMITkeywords and condition
  1. 对字符串使用单引号
  2. 不要在表名周围加上单引号(使用 ` 代替)
  3. 不要在数字周围加上单引号(可以,但更难阅读)
  4. 不要放在和AND之间ORDER BYLIMIT
  5. 不要把=之间ORDER BYLIMIT关键词和条件

So you query will look like:

所以你的查询看起来像:

SELECT post_datetime 
FROM post 
WHERE type = 'published' 
ORDER BY post_datetime DESC 
LIMIT 3

回答by panther

Try:

尝试:

SELECT post_datetime 
FROM post 
WHERE type = 'published' 
ORDER BY post_datetime DESC 
LIMIT 3

回答by Jens

Remove the quotes here:

删除这里的引号:

is:

是:

ORDER BY = 'post_datetime DESC' AND LIMIT = '3'

Should be:

应该:

ORDER BY post_datetime DESC LIMIT 3