这是我的查询(Mysql)
$query_Search = 'SELECT ID, Titre , Categorie, Description, Province , Com_Territ, Type_Annonce, Prix, Devise, Date, State
FROM articles
WHERE Titre LIKE \'%' . safe( $_POST['q'] ) . '%\' OR Categorie LIKE \'%' . safe( $_POST['q'] ) . '%\'
ORDER BY CASE WHEN Titre LIKE \'' . safe( $_POST['q'] ) . '%\' THEN 0
WHEN Titre LIKE \'%' . safe( $_POST['q'] ) . '\' THEN 1
ELSE 2 END';
我想在WHERE子句中添加一个条件,我的条件是(WHERE State ='3'),我这样做:
$query_Search = 'SELECT ID, Titre , Categorie, Description, Province , Com_Territ, Type_Annonce, Prix, Devise, Date, State
FROM articles
WHERE State='3' AND Titre LIKE \'%' . safe( $_POST['q'] ) . '%\' OR Categorie LIKE \'%' . safe( $_POST['q'] ) . '%\'
ORDER BY CASE WHEN Titre LIKE \'' . safe( $_POST['q'] ) . '%\' THEN 0
WHEN Titre LIKE \'%' . safe( $_POST['q'] ) . '\' THEN 1
ELSE 2 END';
此查询显示一些状态不等于3的结果,似乎它不关心条件(WHERE State ='3')
答案 0 :(得分:0)
就像上面提到的putvande一样,试试这个:
$query_Search = 'SELECT ID, Titre , Categorie, Description, Province , Com_Territ, Type_Annonce, Prix, Devise, Date, State
FROM articles
WHERE State='3' AND (Titre LIKE \'%' . safe( $_POST['q'] ) . '%\' OR Categorie LIKE \'%' . safe( $_POST['q'] ) . '%\' )
ORDER BY CASE WHEN Titre LIKE \'' . safe( $_POST['q'] ) . '%\' THEN 0
WHEN Titre LIKE \'%' . safe( $_POST['q'] ) . '\' THEN 1
ELSE 2 END';
答案 1 :(得分:0)
您的OR
语句中有WHERE
,因此您可以获得具有State = 3
OR的列!别的什么。
添加一些(
和)
以对您的语句进行分组,如:
WHERE State='3' AND (Titre LIKE \'%' . safe( $_POST['q'] ) . '%\' OR Categorie LIKE \'%' . safe( $_POST['q'] ) . '%\')