1

I am not used to working with NULL and i am facing some problems. For example one would think that this query returns all customers:

SELECT * from customers WHERE ( job LIKE '%expert%' OR job NOT LIKE '%expert%' )

But reality is that it won't return customers with a NULL job.

That's an example. A more real world query could be:

SELECT * from customers WHERE NOT ( job LIKE '%expert%' )

And to work properly it could be rewritten like:

SELECT * from customers WHERE NOT ( job IS NOT NULL AND job LIKE '%expert%' )

What's the best way of dealing with this NULL in logical subexpressions? I am mainly a PHP programmer for small scripts (i'm not used to ORM or other frameworks).

LatinSuD
  • 1,779
  • 12
  • 19

2 Answers2

1

You can get the proper results by using this query

SELECT * from customers WHERE NOT ( job LIKE '%expert%' ) OR job IS NULL

vikas pal
  • 26
  • 4
0

I guess COALESCE() is quite handy in most cases.

My last example could be like:

SELECT * from customers WHERE NOT ( COALESCE(job,'') LIKE '%expert%' )

It replaces job with an empty string if it happened to be NULL.

LatinSuD
  • 1,779
  • 12
  • 19