SQL - 如何对使用限制约束的选择查询重新排序 [英] SQL - How to reorder a select query that uses the limit constraint

查看:46
本文介绍了SQL - 如何对使用限制约束的选择查询重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下运行正常的 sql 语句.

I have the following sql statement that is working properly.

SELECT * FROM table WHERE name IS NULL ORDER BY date DESC LIMIT 20;

该查询基本上按日期降序对表进行排序,并选择字段 'name' 为空的最后 20 条记录.问题是检索到的查询是按日期 desc 排序的.这当然是逻辑......但我想要的是,输出"是按日期 asc 排序的.预先感谢您的回复.干杯.马克

The query basically orders the table on date desc, and selects the 20 last records for which the field 'name' is null. The problem is that the query that is retrieved is ordered on date desc. It's logic of course... But what I would like, is that the 'output' is ordered on date asc. Thank you in advance for you replies. Cheers. Marc

推荐答案

试试这个:

SELECT * FROM (
    <your query>
)
ORDER BY date

注意:一些 SQL 方言需要命名子查询,即使该名称并未真正使用,例如:

Note: some SQL dialects require naming the subquery even if that name is not really used, for example:

SELECT * FROM (
    <your query>
) T
ORDER BY date

这篇关于SQL - 如何对使用限制约束的选择查询重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆