我有一张名为' language'如下图所示
personid | lang
-------------------
1 | english
1 | french
1 | italian
2 | italian
3 | french
3 | italian
4 | japanese
我希望用人格1知道的任何语言选择所有人格。 (即任何具有英语,法语或italaian值的人物)
我使用了以下查询并获得了解决方案。但似乎有点慢。 (我认为这是由于' IN'条款)。是否有任何其他查询选项可以加快执行速度。
SELECT distinct personid FROM language WHERE personid!=1 AND lang IN (SELECT lang FROM language WHERE personid=1)
答案 0 :(得分:1)
您可以自行加入language
表:
SELECT DISTINCT l2.personid AS personid FROM
language l1 INNER JOIN language l2
ON l1.lang = l2.lang
WHERE l1.personid = 1;
<强>输出:强>
+----------+
| personid |
+----------+
| 2 |
| 3 |
+----------+
答案 1 :(得分:0)
您可以使用EXISTS
:
SELECT distinct personid
FROM language AS l1
WHERE personid != 1 AND EXISTS (SELECT 1
FROM language AS l2
WHERE personid = 1 AND l1.lang = l2.lang)