简体   繁体   English

从PHP mySQL搜索结果中排除值

[英]Excluding Values From PHP mySQL search results

I have read up on excluding results in PHP and mySQL but I can't seem to figure out the correct syntax for the search code I am using. 我已阅读了有关排除PHP和mySQL中结果的信息,但似乎无法找出我所使用的搜索代码的正确语法。

In all of my other result tables that simply pull information from the database without having to use a search form, excluding userlevels has been easily achieved by simply including the following after the "WHERE" statement: 在我所有的其他结果表中,只需从数据库中提取信息而无需使用搜索表单,就可以通过在“ WHERE”语句之后简单地添加以下内容来轻松实现排除用户级别:

WHERE `userlevel` !='9' AND `userlevel` !='1' AND `userlevel` !='4' AND `userlevel` !='5' AND `userlevel` !='6'

Now, I'm using a search code that uses "OR" instead of "AND" and "LIKE" with the terms. 现在,我使用的搜索代码使用“ OR”而不是“ AND”和“ LIKE”。 It looks like this: 看起来像这样:

//Set the MySQL query
if ($search == NULL or $search == '%'){
} else {
for ($i=0; $i<count($keywords); $i++) {
$query = "SELECT * FROM sound_users " .
"WHERE genre LIKE '%".$keywords[$i]."%'".
" OR genre2 LIKE '%".$keywords[$i]."%'" .
" OR genre3 LIKE '%".$keywords[$i]."%'" .
" OR bandname LIKE '%".$keywords[$i]."%'" .
" ORDER BY genre";

}

I saw in some other forums that "NOT IN (SELECT *)...." is being used to exclude data but it seems like every code uses punctuation differently - has a different syntax - so I'm not so sure how to exclude specific values with this search code. 我在其他一些论坛上看到“ NOT IN(SELECT *)....”被用于排除数据,但似乎每个代码使用的标点符号都不同-具有不同的语法-因此我不确定如何排除此搜索代码的特定值。

I have tried simple angles like: 我尝试过像以下这样的简单角度:

" OR userlevel !='5'" .
" OR userlevel !='9'" .

and that only did funky things to the search results which wound up including both of those userlevels at the same time in two different tables stacked on top of eachother. 而这只会对搜索结果产生一些奇怪的影响,这些搜索结果同时包含两个用户级别,同时又放在彼此叠放的两个不同表中。 So I changed the "OR" to "AND" - like it is in my other code and that didn't work either. 因此,我将“ OR”更改为“ AND”-就像我的其他代码中那样,并且也不起作用。

At that point, I tried to add the "NOT IN" and "NOT EXISTS" examples I found but any way I do it, always seems to come back with san error telling me to check the syntax. 那时,我尝试添加我发现的“ NOT IN”和“ NOT EXISTS”示例,但是无论如何,总是会出现san错误,告诉我检查语法。

I'm wondering if it's even possible to exclude specific values from search results using "OR" or if exclusion only works for basic code simply calling values from the database to display and not necessarily using a search form. 我想知道是否甚至可以使用“或”从搜索结果中排除特定的值,或者排除是否仅适用于简单的代码,只需从数据库中调用要显示的值而不必使用搜索表单。

Parentheses are your friend! 括号是您的朋友!

WHERE 
  (X LIKE '%X%' OR Y LIKE '%Y%' OR Z LIKE '%Z%')
  AND
  (X NOT LIKE '%XX%' OR Y<>'abc' OR LENGTH(Z)<5 OR A<>7)

BTW: The usual notation for unequality in SQL is <> , not != 顺便说一句:SQL中不平等的通常表示法是<> ,而不是!=

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

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