gpt4 book ai didi

mysql - 连接同一张表中的两条sql语句

转载 作者:行者123 更新时间:2023-11-30 23:06:25 24 4
gpt4 key购买 nike

我有一个类似的表,

Class  City
01 xx
02 xx
03 yy
04 zz

我想要的结果是,(选择 Class and Count of City where city = xx or city = yy )

Class  Count
01 2
02 2
03 1
04 0

我无法理解如何将不匹配的行也放入结果中。在这个例子中,最后一行是 04 类。

感谢任何帮助

最佳答案

尝试以下 SQL:

SELECT t1.class, count(t2.class) AS COUNT FROM test t1
LEFT JOIN test t2 ON t1.city=t2.city AND t1.city in ('xx', 'yy')
GROUP BY t1.class, t1.city;

SQL Fiddle

关于mysql - 连接同一张表中的两条sql语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21676406/

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