简体   繁体   中英

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. As if there were no empty rows in the table, but there are empty ( NULL ) rows in the thing2 row of the table.

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 | Something | Date | Thing1 | 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

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.

something <> NULL will evaluate to NULL . And if used in a query that's pretty much the same as 0 (or false ).

Remember that SQL uses three-valued logic and not simple binary logic.

You might need to check for NULL explicitly using 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 ):

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

I accept @JoachimSauer's way. Also, I prefer to try below one too.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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