PHP MYSQL搜索特定的行

时间:2011-03-15 18:10:27

标签: php mysql

我想仅在某些特定行之间进行查询。 (查询word = title,但数据仅在第8行,第9行,第10行之间)。但是我的代码仍然会将所有结果都返回。(它将打印第33行,第36行...)如何进行正确的查询?谢谢。

$where = " field_1 = '$word' OR field_2 = '$word' OR field_3 = '$word' ";
SELECT * FROM table WHERE $where AND (id='8' OR id='9' OR id='10');

2 个答案:

答案 0 :(得分:6)

AND的优先级高于OR,所以你需要围绕$where的括号:

SELECT * FROM table WHERE ($where) AND (id='8' OR id='9' OR id='10');

如果没有括号,则评估为:

SELECT *
FROM table
WHERE field_1 = '$word' OR
      field_2 = '$word' OR
      (field_3 = '$word' AND (id='8' OR id='9' OR id='10'))

答案 1 :(得分:0)

$where = " field_1 = '$word' OR field_2 = '$word' OR field_3 = '$word' ";
SELECT * FROM table WHERE $where AND (id='8' OR id='9' OR id='10') LIMIT 8,3;

那应该抓住从第8行开始的3条记录。这有用吗?