我有一个表,links1,其列标题为CardID和AbilityID,如下所示:
CardID | AbilityID
1001 | 1
1001 | 2
1001 | 3
1002 | 2
1002 | 3
1002 | 4
1003 | 3
1003 | 4
1003 | 5
我想要的是能够返回具有两个特定AbilityID的所有CardID。
例如:
如果我选择1和2,则返回1001.
如果我选择3和4,则返回1002和1003。
是否可以只使用一个表来执行此操作,还是需要创建相同的表并对其进行内部联接?
答案 0 :(得分:6)
尝试自我加入:
SELECT t1.CardID
FROM links1 AS t1
JOIN links1 AS t2
ON t1.CardID = t2.CardID
WHERE t1.AbilityID = 1 AND t2.AbilityID = 2;
答案 1 :(得分:2)
You can try nested query,in your case
SELECT cid FROM
(
SELECT cid,COUNT(cid) AS total FROM links WHERE aid = 1 OR aid = 2
GROUP BY cid
) AS t1 WHERE total%2 = 0;