SQL:使用 UNION、ORDER BY 和 LIMIT 进行 SELECT

发布于 2024-10-12 16:41:42 字数 363 浏览 5 评论 0原文

我收到的错误是 ORDER by 应该在 UNION 之后出现,但我希望这些查询在合并为 1 之前排序,然后限制为 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'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 ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜无邪 2024-10-19 16:41:42

如果你在 SQLite 中有一个非常复杂的查询,但需要使用 UNION 进行排序,那么你可以尝试

select * from (
    select * from b ORDER BY date asc
    )
UNION
select * from (
    select * from b ORDER BY name desc
    )
UNION
select * from (
    select * from b ORDER BY gender asc
    )

if you have a very complex query in SQLite but need to use UNION with ordering, then you can try

select * from (
    select * from b ORDER BY date asc
    )
UNION
select * from (
    select * from b ORDER BY name desc
    )
UNION
select * from (
    select * from b ORDER BY gender asc
    )
终难愈 2024-10-19 16:41:42

订单将影响整个联盟。

无论如何,看起来您想要最接近 now 的行。你可以试试这个:

SELECT   time, x, y, z 
FROM     db 
ORDER BY ABS(time - now) ASC
LIMIT    10

An order by will affect the ENTIRE union.

Anyway, it looks like you want the rows nearest to now. You could try this:

SELECT   time, x, y, z 
FROM     db 
ORDER BY ABS(time - now) ASC
LIMIT    10
仅此而已 2024-10-19 16:41:42

在“标准”(对于“标准”的某些定义)SQL;

select top 10 *
from (       select time,x,y,z from db where time > now
       union select time,x,y,z from db where time < now
     ) t
order by t.time

如何限制结果集中的行数可能因 SQL 实现而异。

In "standard" (for some definition of "standard") SQL;

select top 10 *
from (       select time,x,y,z from db where time > now
       union select time,x,y,z from db where time < now
     ) t
order by t.time

How to limit the number of rows in the result set may vary between SQL implementations.

美人如玉 2024-10-19 16:41:42

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

请参阅 SQL Server:在子查询中使用 UNION 进行 ORDER BY 了解绕过这个。

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.

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

七秒鱼° 2024-10-19 16:41:42

我应用的任何简单解决方案如下:

SELECT 
 name,
 TO_DATE(date1, 'DD-MON-YYYY HH:MI AM')
FROM TableX

UNION

SELECT
 name,
 TO_DATE(date2, 'DD-MON-YYYY HH:MI AM')
FROM TableY

ORDER BY 2 DESC;

这按第二(日期)列对结果进行排序。

如果日期列是字符串,则可以应用上面所示的 TO_DATE 函数,否则没有必要。

Any easy solution I applied was as follows :

SELECT 
 name,
 TO_DATE(date1, 'DD-MON-YYYY HH:MI AM')
FROM TableX

UNION

SELECT
 name,
 TO_DATE(date2, 'DD-MON-YYYY HH:MI AM')
FROM TableY

ORDER BY 2 DESC;

This orders the results by the second (date) column.

If the date column is a string, you can apply the TO_DATE function as shown above, else it is not necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文