mysql时间排序最后一个,MYSQL-按日期对它们进行排序后,获取最后5行

I'm trying to execute a MYSQL command which grabs all the rows with the given UserID, sorts them by date, then returns only the first 5.

The command to sort is

ORDER BY date

and the command to get the last 5 is

WHERE ROWNUM <= 5

The WHERE comes before the ORDER, so it's backwards. So I figured I have to have a Mysql statement within a mysql statement.

Here is my attempt. I was getting an alias error, so i added the AS T1 to the command.

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;

Any ideas?

解决方案

As you are working with MySQL, why not use the LIMIT clause, to only keep the first 5 results ?

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值