Question

I have a table linksbase that is filled with values. lastvisited is datetime and is NULL everywhere (nulls are allowed). I run query in phpmyadmin (same via Java) and go empty result:

SELECT `id` FROM `linksbase` WHERE `lastvisited` = NULL

What's more, if I run

SELECT `id` FROM `linksbase` WHERE `lastvisited` != NULL 

I also get emplty result. If I run

SELECT * FROM linksbase WHERE 1

I get a list of rows like should be (so, it's working).

What's wrong with the query and why two mutually excluding queries return 0 rows.

Was it helpful?

Solution

You should use

SELECT `id` FROM `linksbase` WHERE `lastvisited` IS NULL

and

SELECT `id` FROM `linksbase` WHERE `lastvisited` IS NOT NULL

OTHER TIPS

You should be using IS NULL or IS NOT NULL instead of comparing NULL with ! or != operators. Those will always be false.

if you have empty columns and not null , then consider to use this

 SELECT `id` FROM `linksbase` WHERE  `lastvisited` = ''  OR `lastvisited` IS NULL

this will look for empty columns and columns with null .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top