我正在尝试获取所有值等于 hello & world 的 ID。 既然我有这个数据结构,我怎样才能得到这个数据?
id value
1 hello
1 world
2 hai
3 hello
3 world
预期输出
id
1
3
最佳答案
你需要一个 self join
上id
检查是否有相同 id
的记录有hello
和world
在value
专栏。
以下查询将为您提供所需的输出。
select t1.id
from @table t1
inner join @table t2
on t1.id = t2.id
where t1.value = 'hello'
and t2.value = 'world'
输出
+----+
| id |
+----+
| 1 |
+----+
| 3 |
+----+
关于mysql - 如何根据and条件获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55034074/