我有这样的记录....
oid id
35 1
43 1
46 1
43 2
49 2
50 3
51 3
52 4
我有 id=1 和 2 。我想要那些只属于两个 ids(1,2) 的结果。
即 - o/p = recordds of object_id = 43 因为它同时属于 1 和 2。
如果我使用 in 运算符,那么它会给出所有记录(执行 OR 运算)
最佳答案
这是针对此类问题的通用快速解决方案。
获取满足我们条件的所有 ID(在本例中为 oid 值):
select oid from MyTalbe where id in (1,2) group by oid having count (distinct id) >1
在我们需要的 ID 列表中选择 ID('oid` 列)所在的行:
从Mytable中选择oid,id oid 在哪里 (从 MyTalbe 中选择 oid where id in (1,2) -- 这等同于:where id =1 or id=2 按oid分组 有 count (distinct id) >1 )
关于mysql - In 从句与 And 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17167308/