simond simond - 6 months ago 8
SQL Question

SQL WHERE is number or null

I am trying to write a sql query that should allow a specified number and/or NULL value within a WHERE clause. Here is what I have written so far:

SELECT *
FROM sd_menus a
LEFT JOIN sd_menu_items b
ON b.id_menu = a.id
LEFT JOIN `sd_menu_items_value_lang` c
ON c.item_id = b.id
WHERE a.name = 'mainNav'
AND (lang_id = 2 OR lang_id = NULL)


I think that the reason the above is not working, is because of the OR statement, and that in this case I need And/Or. I guess you can do that with SQL, but how? :)

Answer

You must use Is null and not = null

SELECT * 
FROM sd_menus a
LEFT JOIN sd_menu_items b
ON b.id_menu = a.id
LEFT JOIN `sd_menu_items_value_lang` c
ON c.item_id = b.id
WHERE a.name = 'mainNav'
AND (lang_id = 2 OR lang_id is  NULL)