如何在同一个字段中的两个值的where子句?

时间:2017-12-13 17:54:23

标签: php mysql sql database

我有3张桌子

物体

ID || NAME
====================
1  || object title 
--------------------
2  || name of object
etc..

道具

ID || NAME
===========
1  || draft
-----------
2  || views
etc..

obj_props

ID || OID || PID || VALUE
=========================
1  || 1   || 1   || 1
-------------------------
1  || 1   || 2   || 70
etc..

我需要选择 draft = 1和 views >的记录0

我当前的mysql查询:

SELECT o.id, p.name as prop_name, op.value as prop_val 
FROM objects o 
INNER JOIN obj_props op ON o.id = op.object_id 
INNER JOIN props p ON p.id = op.prop_id 
WHERE p.name = 'views' and op.value > 0 

但是当我添加

AND p.name = 'draft' and op.value = 1

它返回null。

如果用OR代替AND运算符 - 代码返回所有记录,但我需要单个对象的搜索道具。

1 个答案:

答案 0 :(得分:6)

您需要使用OR 括号

WHERE (p.name = 'views' AND op.value > 0)
   OR (p.name = 'draft' AND op.value = 1)