gpt4 book ai didi

sqlite - 如何在计数中显示0?

转载 作者:行者123 更新时间:2023-12-03 17:08:36 26 4
gpt4 key购买 nike

没有资格匹配怎么显示0?

我当前的查询是:

select s.sname, count(*) as number
from sailor s, boat b
where s.rating >= b.rating
group by s.sname
order by number desc;

我得到的结果是:

a  3
b 3
c 2

然而,这并不是我想要实现的。我想这样显示结果:

a 3
b 3
c 2
d 0
e 0

我尝试将 count() 更改为 isnull(count(), 0) 或 coalesce(count(*), 0) 但在这种情况下它不正确。

CREATE TABLE sailor (
sname character varying primary key,
rating integer
);

create table boat (
bname character varying primary key,
color character varying,
rating integer
);

最佳答案

这可能对你有帮助

select s.sname,(select count(*) 
from boat b
where s.rating >= b.rating
) t
from sailor s
order by t desc;

SQLFIDDLE

关于sqlite - 如何在计数中显示0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26695780/

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