我们在mysql查询中使用了多少和/或者

时间:2013-02-07 17:46:11

标签: mysql

我正在制作一个搜索表单,无论我做什么,我的查询都会返回错误的结果。

$sql = "select * from posts WHERE title like '%$term%' or description like '%$term%' and type='$type' order by id";

这是返回结果直到描述但完全忽略了类型。但如果我只提供它的工作类型。例如

$sql = "select * from posts WHERE type='$type' order by id";

任何人都可以告诉我这是因为查询过于复杂吗?或者我错过了什么。提前谢谢。

1 个答案:

答案 0 :(得分:4)

好像你需要一些括号,因为AND优先于OR

$sql = "select * 
from posts 
WHERE (title like '%$term%' or description like '%$term%') and type='$type' order by id";

指定列列表而不是SELECT *也是一种好习惯。

相关问题