我需要在键值表中获取具有以下结构的记录:
CREATE TABLE `PROPERTY` (
`id` int(11) NOT NULL,
`key` varchar(64) NOT NULL,
`value` text NOT NULL,
PRIMARY KEY (`id`,`key`)
);
我需要获取具有MULTIPLE特定键值条目的所有ID。例如,所有具有键“foo”,“bar”和“foobar”的ID。
答案 0 :(得分:2)
只需使用GROUP BY
进行分组,然后检查组计数即可计算多个值:
Select
id
from
`PROPERTY`
group by
key, value
having
count(*) > 1
答案 1 :(得分:0)
I f you need the id of all the rows for key,value count(= >1)
select id from `PROPERTY`
where (key, value) in (select key, value from `PROPERTY`group by
key, value
having
count(*) > 1)
答案 2 :(得分:0)
鉴于更新的问题...
既然您知道具体的密钥,那么您也知道有多少密钥...所以有一个不同的计数应该这样做...以及在哪里......
SELECT id
FROM `PROPERTY`
Where key in ('foo','bar','foobar')
GROUP BY ID
having count(distinct key) = 3