我搜索了一个MySQL解决方案来收集查询,其中包含一些int(数组中的id)。
Example-Table: id1 id2 id3 1 2 2 2 3 2 3 3 5 4 4 2 5 4 5 6 4 7
我有一个数组
$id3array = (2,5);
我需要id2=3(不是4,因为id2“4”还有一个id3参数(7)
如果我有一个类似的数组
$id3array = (2,5,6); // array can contains 20 and more arguments
我对IN(2,5)或Group by的测试没有成功-这可以用MySQL解决吗?
http://www.sqlfiddle.com/#!9/b71306/2
尝试以下查询。放入将匹配不在所需数组中的记录的内部查询。然后对精确元素进行分组和计数
SELECT `id2` FROM `exampletable` WHERE `id3` IN (2,5) AND `id2` NOT IN ( SELECT e.id2 from exampletable e WHERE e.id3 not in (2,5)) GROUP BY `id2` HAVING count(`id2`) = 2;
我在这里补充了 计数( id2 ) = 2 因为数组中有2个元素
id2
DEMO