SQL:使用UNION,ORDER BY和LIMIT进行SELECT [英] SQL: SELECT with UNION, ORDER BY and LIMIT

查看:285
本文介绍了SQL:使用UNION,ORDER BY和LIMIT进行SELECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到UNION之后出现ORDER by的错误,但是我希望这些错误在组合为1然后限制为10之前进行排序.

I'm getting Errors that ORDER by should come after UNION but i want these to queries ordered before combined to one and then limited to 10.

SELECT * 
  FROM (SELECT time, x, y, z 
          FROM db 
         WHERE time >= now 
      ORDER by time, x
       UNION 
       SELECT time, x, y, z 
         FROM db 
        WHERE time < now 
     ORDER by time, x) 
LIMIT 10

我希望你能理解,我正在努力做些什么并且可以帮助我;-)

I hope you understand, what I'm trying to do and can help me ;-)

推荐答案

这不是它的工作原理,至少在MySQL中(您未指定).在选择数据并执行了所有UNION,GROUP BY等之后,才进行ORDER操作.

That's not how it works, at least in MySQL (you didn't specify). The ORDER operation comes after the data is selected and all UNIONs, GROUP BYs, etc. have been performed.

请参见> SQL Server:在带有UNION的子查询中进行ORDER BY 解决此问题的方法.

See SQL Server: ORDER BY in subquery with UNION for a way around this.

这篇关于SQL:使用UNION,ORDER BY和LIMIT进行SELECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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