Similar to my chart of IS NULL expressions for CodeIgniter's query builder methods, below is a battery of good, bad, and ugly IS NOT NULL expressions.
You may be surprised to see that ->where('column is not ') works perfectly, but simply removing the trailing space renders a completely unsuitable expression.
Perhaps more surprisingly, ->where('column !=') is magically rendered as `column` IS NOT NULL.
| Quality |
Syntax |
SQL |
| ⭐ |
->where('column is not null') |
`column` is not null |
| ✅ |
->where('column !=') |
`column` IS NOT NULL |
| ✅ |
->where('column !=', null) |
`column` IS NOT NULL |
| ✅ |
->where('column is not ') |
`column` IS NOT NULL |
| ✅ |
->where('column is not null', null) |
`column` is not null |
| ✅ |
->where(['column is not ' => null]) |
`column` IS NOT NULL |
| ✅ |
->where(['column is not null' => null]) |
`column` is not null |
| ⚠️ |
->where('column is not null', null, false) |
column is not null |
| ⚠️ |
->where('column is not', 'null', false) |
column is not null |
| ⚠️ |
->where('column is not ', null, false) |
column IS NOT NULL |
| ❌ |
->where('column is not') |
column is not |
| ❌ |
->where('column is not', 'null') |
column is not 'null' |
| ❌ |
->where('column is not', null, false) |
column is not |
| ❌ |
->where('column is not null', 'null') |
`column` is not null 'null' |
| ❌ |
->where('column', 'is not null') |
`column` = 'is not null' |
| ❌ |
->where('column', 'is not null', false) |
column = is not null |
| ❌ |
->where('NOT column ') |
NOT column |
| ❌ |
->where('NOT column IS', null) |
NOT column IS |
| ❌ |
->where('NOT column <=>') |
`NOT` `column` <= `>` |
| ❌ |
->where(['col is not' => null]) |
col is not |
| ❌ |
->where(['col is not' => null], null, false) |
col is not |
⭐ is my recommendation, ✅ means correct and fully quoted, ⚠️ means it will work but not fully quoted, ❌ means it is incorrect
When using get_where(), the second parameter can be a string or an associative array, but you cannot express escape = false.