I’m trying to learn how to use CASE/WHEN in SQL more specifically, mySQL.
I have two tables: adhoc_item and adhoc_vulnerability. ahdoc_item has one adhoc_vulnerability related to it. now adhoc_vulnerability may or may not have a vulnerability (original_vulnerability_id) attached to it.
In case it doesn’t, then I want to return the name on itself.
The tables: (or relevant parts thereof)
ADHOC_ITEM

ADHOC_VULNERABILITY

This is my query:
SELECT adi.name as item, CASE ahv.original_vulnerability_id
WHEN ISNULL(ahv.original_vulnerability_id) THEN ahv.name
ELSE 'foo'
END NAME
FROM adhoc_item adi
JOIN adhoc_vulnerability ahv ON ahv.id = adi.adhoc_vulnerability_id
and these are the results!

Now I know this is a little bit complex but i’ve been thinking about it for about 2 hours and found no explanation on why that last item (which has an adhoc_vulnerability with NULL as original_vulnerability_id) shows the value that is supposed to appear precisely when that attribute is not null!
Thanks in advance.
The problem is that when
ahv.original_vulnerability_idisNULL, theISNULL()function returnsTRUE(which is MySQL is the same as1but this is irrelevant). So, instead of comparing the column withNULL, you are comparing it withTRUE.Rewrite the
CASEexpresion as:or as: