简体   繁体   English

SQL运算子:AND和OR

[英]SQL Operators: AND and OR

I'm just wondering if you can use both of them in a PHP code. 我只是想知道您是否可以在PHP代码中同时使用它们。 I thought something like this: 我认为是这样的:

$sql2 = mysql_query("SELECT * FROM forum WHERE id='$topicsnumber' AND main='0' OR main='1' OR main='2'");   
while($row=mysql_fetch_array($sql2)) {code in here}

So that it checks it like this WHERE id='$topicsnumber' AND (main='0' OR main='1' OR main='2'). 这样它就可以像WHERE id ='$ topicsnumber'AND(main ='0'OR main ='1'OR main ='2')这样进行检查。 Is this possible? 这可能吗?

Yes, it's possible with parentheses around the conditions, but it's better written as 是的,可以在条件周围加上括号,但最好写成

mysql_query("SELECT * FROM forum WHERE id='$topicsnumber' AND main IN ('0','1','2')");

That way you don't have to worry about operator precedence. 这样,您不必担心运算符的优先级。

If main is a numeric data type, you can drop the apostrophes around the numbers in your query. 如果main是数字数据类型,则可以在查询中的数字前后加上撇号。

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

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