我有一个像这样的mysql表
+-----+---------+-----------+-----------------+-------+
| id | item_id | item_type | field_name | data |
+-----+---------+-----------+-----------------+-------+
| 258 | 54 | page | field_interests | 1 |
| 257 | 54 | page | field_interests | 0 |
| 256 | 54 | page | field_author | value |
+-----+---------+-----------+-----------------+-------+
而且,我需要像这样构建查询
SELECT * FROM `content_fields_data` WHERE
(`field_name`='field_author' AND `data` LIKE '%lu%')
AND
(`field_name`='field_interests' AND `data`='1')
但它返回0行。如果我改变它或
SELECT * FROM `content_fields_data` WHERE
(`field_name`='field_author' AND `data` LIKE '%lu%')
**OR**
(`field_name`='field_interests' AND `data`='1')
I need to select all rows
WHERE
field_name.field_author.data LIKE '%text%'
AND
field_name.field_interests.data = '1'
我可以解释它就像一个简单的函数:
IF table.field_name = 'field_author' THAN USE LIKE operator
IF table.field_name = 'field_interests' THAN USE equal(=) operator
任何人都可以帮助我吗?
感谢。
答案 0 :(得分:0)
我认为你必须使用OR而不是AND(否则很明显你没有行!一件事情不能等于两件事......)。所以
SELECT * FROM table WHERE
(`field_name`='field_author' AND `field_author.data` LIKE '%jo%')
OR
(`field_name`='field_interests' AND `field_interests.data`='0')
OR
(`field_name`='field_interests' AND `field_interests.data`='1')