我加入了一些表格,以便我有这样的结果表
<table border="1" style="border-collapse: collapse">
<tr><th>ID</th><th>name</th><th>val</th></tr>
<tr><td>1</td><td>n1</td><td>false</td></tr>
<tr><td>1</td><td>n2</td><td>true</td></tr>
<tr><td>1</td><td>n3</td><td>true</td></tr>
<tr><td>2</td><td>n1</td><td>true</td></tr>
<tr><td>2</td><td>n2</td><td>true</td></tr>
<tr><td>2</td><td>n3</td><td>true</td></tr>
<tr><td>3</td><td>n1</td><td>false</td></tr>
<tr><td>3</td><td>n2</td><td>true</td></tr>
<tr><td>3</td><td>n3</td><td>true</td></tr>
</table>
我需要获取ID,对于此ID n1 = true且n2 = true,因此从该表中只有ID 2才是正确的。 如何针对非预定义的名称和值进行此类请求?
答案 0 :(得分:1)
SELECT id FROM t
GROUP BY id
HAVING SUM(name='n1' AND val='true')>0
AND SUM(name='n2' AND val='true')>0
答案 1 :(得分:1)
可能的解决方案是
select distinct id
from yourTable t1
join yourTable t2
on t1.id = t2.id
where t1.name = n1 and t1.val = 'true' and
t2.name = n2 and t2.val = 'true';