我正在尝试执行一个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