如何从列中具有两个不同值的表中进行选择?

时间:2016-02-01 11:04:09

标签: mysql

=============================================== =========

这是示例db

我只想获得技能栏中同时拥有2和14的用户。答案应该是" 2" enter image description here

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT seekerID
FROM mytable
WHERE skillID IN (2, 14)
GROUP BY seekerID
HAVING COUNT(DISTINCT skillID) = 2
只有在DISTINCTskillID值出现多次的情况下才需要

seekerID个关键字。

答案 1 :(得分:0)

最简单的方法是

select seekerID, count(*) as cnt
from table_name
where skillid in (2,14)
group by seekerID
having cnt = 2

答案 2 :(得分:0)

使用它:

select seekerID from table_name where skillid="2" and seekerID = ( select author from table_name where skillid="14")