mysql order by 日期,MYSQL-按日期排序后最后5行

我正在尝试执行一个MYSQL命令,该命令用给定的UserID抓取所有行,按日期对它们进行排序,然后只返回前5个.

排序命令是

ORDER BY date

获得最后5个的命令是

WHERE ROWNUM <= 5

WHERE在ORDER之前,所以它是向后的.所以我想我必须在mysql语句中有一个Mysql语句.

这是我的尝试.我收到别名错误,所以我将AS T1添加到命令中.

SELECT * FROM

(SELECT voting_id, caption_uid, voting_date, rating FROM voting

WHERE user_id = $inUserID AS T1

ORDER BY voting_date)

WHERE ROWNUM <= 5 AS T2;

有任何想法吗?

解决方法:

在使用MySQL时,为什么不使用LIMIT子句,只保留前5个结果?

select *

from your_table

order by your_column

limit 0, 5

The LIMIT clause can be used to constrain the number of rows

returned by the SELECT statement. LIMIT takes one or two numeric

arguments, which must both be nonnegative integer constants (except

when using prepared statements).

With two arguments, the

first argument specifies the offset of the first row to return, and

the second specifies the maximum number of rows to return.

标签:mysql,sorting,datetime,alias

来源: https://codeday.me/bug/20190714/1455902.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值