我需要编写一个结合了可变数量的AND WHERE语句的查询。
一个陈述看起来像
SELECT name
FROM users
WHERE field_id=16
AND value BETWEEN 1 AND 100
但我需要从可变数量的字段返回一个结果数组,因此field_id为16,18,20,25,每个field_id都有一个特定的" AND值...&#34 ;标准。
我怎么能这样做才能返回一组值?
我也从html表单中获取搜索条件并使用php(wordpress)处理它
答案 0 :(得分:1)
如果field_id
需要与value
字段条件相关,那么您需要AND
field_id
和value
值OR
SELECT name
FROM users
WHERE (field_id=16 AND value BETWEEN 1 AND 100)
OR (field_id=17 AND value between 101 and 199)
OR (field_id=18 AND value between 201 and 299)
OR (...
每套标准。
IN
您可能也在寻找SELECT name
FROM users
WHERE (field_id IN (16,17,18) AND value IN (100,200,300))
...
声明。看起来像这样。
{{1}}
答案 1 :(得分:0)
您可以使用AND和OR条件的组合:
SELECT name
FROM users
WHERE (field_id=16 AND value BETWEEN 1 AND 100)
OR (field_id=18 AND value BETWEEN 1 AND 150)
OR (field_id=20 AND value BETWEEN 10 AND 50)
OR (field_id=25 AND value BETWEEN 100 AND 500)
....