当列必须满足多个值时如何选择行?

时间:2015-11-07 01:10:46

标签: mysql sql

例如,我有一张桌子

name | ability

kevin|say
kevin|scream
nike |say

在寻找sayscream时,我想得到回应。参数计数可能会改变。

3 个答案:

答案 0 :(得分:2)

您可以使用group byhaving

执行此操作
SELECT name
FROM t
WHERE ability in ('say', 'scream')
GROUP BY name
HAVING COUNT(*) = 2;

答案 1 :(得分:0)

一种方法是:

SELECT * FROM YourTableName WHERE name='kevin' AND ability IN ('say','scream');

另一种方法:

SELECT * FROM YourTableName WHERE name='kevin' AND (ability='say' || ability='scream');
祝你好运!!

答案 2 :(得分:0)

这应该有效:

SELECT name FROM tablename WHERE ability IN ('say', 'scream') GROUP BY name

或者:

SELECT DISTINCT name FROM tablename WHERE ability IN ('say', 'scream')
编辑:哦,你正在寻找具有两个能力的名字......那么你可以这样做:

SELECT DISTINCT name from tablename WHERE name IN (SELECT name FROM tablename WHERE ability = 'say') AND name IN (SELECT name FROM tablename WHERE ability = 'scream')

或者更好的是,您可以在第一个查询中添加HAVING子句:

SELECT name FROM tablename WHERE ability IN ('say', 'scream') GROUP BY name HAVING count(*) = 2