我的表看起来像这样 -
现在用articleid = 54
查看最后两条记录我想只选择一次这样的记录 - 我想选择(fieldsid = 1和value = 1)具有相同articleid的所有记录以及(fieldsid = 2和value = 1)
articleid
必须相同,但它会在两个不同的记录中检查所需的值。
请有人帮助我。
我想这样做 -
select n1.id, n1.fieldsid, n1.value
from `tablename` n1
where (n1.fieldsid='1' and n1.value='1')
and n1.id = (select n2.id
from `tablename` n2
where (n1.fieldsid='2' and n2.value='2')
and n1.id=n2.id)
答案 0 :(得分:2)
SELECT youtable.*
FROM yourtable
WHERE articleid IN (SELECT articleid
FROM yourtable
WHERE (fieldsid, value) IN ((1,1),(2,1))
GROUP BY articleid
HAVING COUNT(*)=2)
编辑:如果您只需要第一条记录:
SELECT youtable.*
FROM yourtable
WHERE id IN (SELECT MIN(id)
FROM yourtable
WHERE (fieldsid, value) IN ((1,1),(2,1))
GROUP BY articleid
HAVING COUNT(*)=2)
答案 1 :(得分:0)
感谢您对fthiella的快速反应。在你的帮助下我把它整理出来 -
SELECT joom_fieldsattach_values.*
FROM joom_fieldsattach_values
WHERE `articleid` IN (SELECT `articleid`
FROM joom_fieldsattach_values
WHERE (`fieldsid`, `value`) IN ((1,1),(2,1))
GROUP BY `articleid`
HAVING COUNT(*)=2)
GROUP BY `articleid`