简体   繁体   English

检查WHERE子句中的条件

[英]Check conditions in WHERE clause

I have a problem with my select query. 我的选择查询有问题。 I have 4 conditions to check this query. 我有4个条件检查此查询。

$subject // This condition is always true that mean it has a value when executing the query
$option // This condition is also true that mean it has a value when executing the query
$district // Maybe this is true or not
$city // Maybe this is true or not

Now I need to make select query with with these 4 conditions. 现在,我需要使用这4个条件进行选择查询。

Can I know is there any way to add these 4 conditions to my WHERE clause and then check which are true or not and then return records according to the condition that query met. 我可以知道有什么方法可以将这4个条件添加到我的WHERE子句中,然后检查是否为真,然后根据查询满足的条件返回记录。

Hope someone will help me. 希望有人能帮助我。

Thank you. 谢谢。

Sure - connect them with AND or OR 当然-将它们与ANDOR连接

SELECT * FROM .. WHERE subject=? AND ...

Hope, you escape the input(PDO do this job for you, if you use prepared statements) 希望您可以避免输入(如果使用准备好的语句,PDO会为您完成此工作)

how about this? 这个怎么样?

WHERE   $subject = 1 AND
        $option = 1 AND
        $district IN (0,1) AND
        $city IN (0,1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM