假设如下表:如何找到行,其中同一列只有同另一列
Name Age Occupation
Alex 20 Student
Alex 20 Seller
Alex 20 Minister
Liza 19 Student
Liza 20 Volunteer
Liza 21 HR partner
我想找到具有唯一(只)20年龄列名。所以从这张桌子我想要所有“亚历克斯”行,并没有“丽莎”行。 谢谢!
假设如下表:如何找到行,其中同一列只有同另一列
Name Age Occupation
Alex 20 Student
Alex 20 Seller
Alex 20 Minister
Liza 19 Student
Liza 20 Volunteer
Liza 21 HR partner
我想找到具有唯一(只)20年龄列名。所以从这张桌子我想要所有“亚历克斯”行,并没有“丽莎”行。 谢谢!
您需要使用Group By
和Having
子句。试试这个方法只
select Name
from table
group by Name
having count(case when Age = 20 then 1 end) = count(*)
count(case when Age = 20 then 1 end)
计数时age = 20
如果它等于总数则名只有20
年龄。使用NOT IN()
方式一:
SELECT Name, Age, Occupation
FROM YourTable
WHERE Age = 20
AND Name NOT IN (SELECT Name FROM YourTable WHERE Age <> 20)
另一种正确的方法。谢谢! –
只要一另一种方式:
select Name
from table
group by Name
having min(Age) = 20 and max(Age) = 20
完美的作品。谢谢! –