I am making the following select query :
select date(al.created_on) date, a.name appname, u.regcode rc, u.email_address ea from audit_logs al
JOIN users u ON al.user_id = u.id
JOIN applications a ON al.application_id = a.id
group by date, appname, ea
order by date DESC, appname, ea;
and get over 900 results where a lot of the values for “u.regcode rc” are NULL.
I also need to filter a specific regcode out and change the query to:
select date(al.created_on) date, a.name appname, u.regcode rc, u.email_address ea from audit_logs al
JOIN users u ON al.user_id = u.id
JOIN applications a ON al.application_id = a.id
WHERE u.regcode != '9999'
group by date, appname, ea
order by date DESC, appname, ea;
But now all the results where the u.regcode was NULL is being filtered out too.
Even if I change the WHERE clausel to ” WHERE u.regcode = NULL “, the NULL values are being filtered out and I get nothing at all.
Is there a way where I can just filter out specific values of the u.regcode-column without losing the lines with the NULL values?
All comparisons to null evaluate to false (or, more precisely, they evaluate to unknown, which means they do not evaluate to true, so that means that they act like false but any other boolean operations using those expressions also evaluate to unknown), including equality and inequality comparisons. This is one of the trickier aspects of SQL to grasp, but fundamentally
nullrepresents a missing value. As far as the SQL server is concerned, there may be a value, but it doesn’t have it, and thus the value is unknown. Because it doesn’t know what the value is, it doesn’t know if it’s equal to, less than, or greater than any other value.To compare a value to null, you need to use either
is nulloris not null.So, for your query, you’d use