gpt4 book ai didi

sql - 联合查询的结果顺序

转载 作者:行者123 更新时间:2023-12-01 10:06:03 25 4
gpt4 key购买 nike

我有一个联合查询,它从两个不同的群体中获取计数。如何强制按照查询中写入的顺序而不是升序/降序返回结果?

select count(datediff(yyyy,dob,admitdate) as counts
from myTable
where condition
union
select count(datediff(yyyy,dob,admitdate) as counts
from myTable
where condition

我希望第一个结果始终作为第一行返回。这可能吗?

最佳答案

您必须使用 ORDER BY 子句指定顺序。在您的示例中,您可能会这样做:

SELECT 1 AS seq, COUNT(datediff(yyyy,dob,admitdate) as counts
FROM myTable
WHERE ...condition-1...
UNION
SELECT 2 AS seq, COUNT(datediff(yyyy,dob,admitdate) as counts
FROM myTable
WHERE ...condition-2...
ORDER BY seq

关于sql - 联合查询的结果顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10555603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com