SQL 运算符:AND 和 OR
我只是想知道是否可以在 PHP 代码中使用它们。我想的是这样的:
$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}
这样它就可以像这样检查 WHERE id='$topicsnumber' AND (main='0' OR main='1' OR main='2')。 这可能吗?
I'm just wondering if you can use both of them in a PHP code. 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').
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,可以在条件两边加上括号,但最好写成这样,
您就不必担心运算符优先级。
如果 main 是数字数据类型,您可以在查询中删除数字两边的撇号。
Yes, it's possible with parentheses around the conditions, but it's better written as
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.