我有下表称为商店:
Name | Category | Industry
ABC appliances retail
XYZ banking finance
NZE clothing retail
JKI tutoring education
我想输出所有在其行业中唯一的名称(例如XYZ和JKI是其行业中唯一的名称)。
我有以下查询:
select s.Name, s.Industry, a.Number
from Stores s
inner join (
select Industry, count(*) as Number
from Stores group by Industry
) a
on s.Industry = a.Industry;
我得到一个输出表,该表具有名为Number的属性,该属性给出每个行业出现在表Stores中的总次数。使用内部联接后,如何在“数字”列中选择所有值为1的元组?
答案 0 :(得分:1)
使用where
条件
select s.Name, s.Industry, a.Number
from Stores s
inner join (
select Industry, count(*) as Number
from Stores group by Industry
) a
on s.Industry = a.Industry where a.Number=1
答案 1 :(得分:1)
使用相关子查询
=
答案 2 :(得分:1)
您可以使用SELECT
:
EXISTS
答案 3 :(得分:1)
我只使用聚合:
select industry, max(name) as name
from stores
group by industry
having count(*) = 1;
如果只有一个名字,那么max(name)
是那个名字。
如果表中可以重复name
,则:
having min(name) = max(name)