我有一个看起来像这样的表:
+----+--------+-------+
| id | entity | word |
+----+--------+-------+
| 1 | 1 | red |
| 2 | 1 | green |
| 3 | 1 | blue |
| 4 | 2 | car |
| 5 | 2 | truck |
| 6 | 2 | train |
| 7 | 3 | water |
| 8 | 3 | milk |
| 9 | 3 | soda |
+----+--------+-------+
如果我搜索blue
,我希望red
,green
和blue
作为答案。现在我正在使用2个查询。一个用于查找“实体”编号,另一个用于查找具有相同“实体”编号的所有单词。
答案 0 :(得分:2)
试试这个。加入比子查询快得多
select distinct t2.word from Table t1
INNER JOIN Table t2 on t2.entity=t1.entity
where t1.word="blue";
答案 1 :(得分:1)
SELECT *
FROM TABLE_NAME
WHERE entity IN
(SELECT entity
FROM TABLE_NAME
WHERE word='blue');