简体   繁体   English

MySQL不等于不工作

[英]MySQL not equals to isn't working

This is my code:这是我的代码:

$query = mysql_query("SELECT P_Id, Thing,Something Date
    FROM priv
    WHERE Something = '$thing' AND thing2 <> 'This'
    LIMIT 1"
) or die(mysql_error());

Code is working properly without "thing2 <> 'this'" statement, but when I implement "thing2 <> 'this'" , it returns 0 result.代码在没有"thing2 <> 'this'"语句的情况下正常工作,但是当我实现"thing2 <> 'this'" ,它返回 0 结果。 As if there were no empty rows in the table, but there are empty ( NULL ) rows in the thing2 row of the table.好像表中没有空行,但表的 thing2 行中有空( NULL )行。

The problem is, that this "not equals" statement is not working, and I've tried everything, but it won't return any values with it.问题是,这个“不等于”语句不起作用,我已经尝试了所有方法,但它不会返回任何值。

edit(word of explanation): Columns looks like this - P_Id |编辑(解释词):列看起来像这样 - P_Id | Something |东西| Date |日期 | Thing1 |事情 1 | Thing 2 and i need to receive P_Id from a row where Something = Something AND Thing1 NOT EQLAS to 'this'(above example) and Thing2 ALSO NOT EQUALS to 'this'.I know it sounds crazy.Sorry Thing 2 和我需要从某行接收 P_Id,其中Something = Something AND Thing1 NOT EQLAS 为“this”(上例),Thing2 也不等于“this”。我知道这听起来很疯狂。对不起

Ok, so i have tried some experiments, and it apears that "thing2 <>" thingy is not working on things1/thing2 columns, but other columns like "Something" table where are also 'this' entries it works properly(finding nerest row where there is not 'this' entry).But why this isn't working with things1 and 2???i have tried rename things column, but no result.好的,所以我尝试了一些实验,结果表明“thing2 <>”thingy 不适用于things1/thing2 列,但其他列如“Something”表中的“this”条目也可以正常工作(找到最靠近的行没有“这个”条目的地方)。但是为什么这不适用于things1 和2?我尝试重命名things 列,但没有结果。

something <> NULL will evaluate to NULL . something <> NULL将评估为NULL And if used in a query that's pretty much the same as 0 (or false ).如果在查询中使用,则与0 (或false )几乎相同。

Remember that SQL uses three-valued logic and not simple binary logic.请记住,SQL 使用三值逻辑而不是简单的二进制逻辑。

You might need to check for NULL explicitly using IS NULL :您可能需要使用IS NULL显式检查IS NULL

... OR THING IS NULL

Alernatively you can use the NULL-safe equals operator <=> with a negation (this is MySQL specific, however, it's not standard SQL ):或者,您可以使用带有否定的NULL 安全等于运算符<=> (这是 MySQL 特定的,但是, 它不是标准 SQL ):

... AND NOT (THING <=> 'This')

I accept @JoachimSauer's way.我接受@JoachimSauer 的方式。 Also, I prefer to try below one too.另外,我也更喜欢尝试以下一种。

... WHERE Something = '$thing' AND NOT (IFNULL(thing2,'') = 'This')
LIMIT 1```

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

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