How to Compare Two Values When One is Null in MySQL

Imagine you're comparing two MySQL columns and you want to know how many rows are different. No problem, you think:

SELECT count(1)
FROM items
WHERE width != height;

Not so fast. If some of the widths or heights are null, they won't be counted! Surely that wasn't your intention. That's where you need a null-aware operator like <=>:

SELECT count(1)
FROM items
WHERE NOT (width <=> height);

Because <=> is actually a null-aware version of the equal operator, we need to negate it with NOT. Now, your count will be "null aware" and you'll get the result you want.

database icon
From MySQL query to chart to Slack in seconds
Get to answers faster, together, with PopSQL and MySQL