无法正确使用此语法:
SELECT DISTINCT id
FROM metadata
WHERE (meta_key = 'school' AND meta_value = 'Some School')
AND WHERE (meta_key = 'hidden' AND meta_value = '1')
它在第4行失败了......
更新: 表看起来像这样:
meta_id - id - meta_key - meta_value
1 1 school Some School 1
2 1 hidden 0
3 2 school Some School 2
4 2 hidden 1
5 3 school Some School 3
6 3 hidden 0
7 4 school Some School 4
8 4 hidden 0
9 5 school Some School 5
10 5 hidden 1
更新:我有一个相关的,在此处延伸Does row exist and multiple where
答案 0 :(得分:5)
您不需要第二个where
,第二个and
可能是or
:
SELECT DISTINCT id
FROM metadata
WHERE (meta_key = 'school' AND meta_value = 'Some School')
OR (meta_key = 'hidden' AND meta_value = '1')
(我认为你想要一个or
的原因是因为你有一个矛盾的条款meta_key = 'school' AND meta_key = 'hidden'
的结合,这总是假的。)
编辑:为了回应OP对他想要获得的结果的评论,这是一个不同的查询:
SELECT DISTINCT m1.id
FROM metadata m1
join metadata m2 on m1.id = m2.id
WHERE (m1.meta_key = 'school' AND m1.meta_value = 'Some School')
AND (m2.meta_key = 'hidden' AND m2.meta_value = '1')
现在联接条款不再矛盾,因为它们引用了两个不同的行m1
和m2
。
答案 1 :(得分:2)
您不能拥有多个WHERE子句。我想你的意思是
SELECT DISTINCT id
FROM metadata
WHERE (meta_key = 'school' AND meta_value = 'Some School')
OR (meta_key = 'hidden' AND meta_value = '1')