mysql if null code example

Example 1: mysql check if not null

SELECT * 
FROM table 
WHERE YourColumn IS NOT NULL;

Example 2: mysql where value is null

SELECT *
FROM contacts
WHERE last_name IS NULL;

Example 3: mysql return 0 if null

SELECT IFNULL(field, 0) AS field FROM products WHERE id = 1

SELECT IFNULL(SUM(field), 0) as field FROM products

Example 4: mysql ifnull

##syntax:
SELECT IFNULL(col_with_null_value, default_value);

##you can see the result select with the string NA where the document is null:
SELECT IFNULL(user_document_id, 'NA');

Example 5: if column value is null then in mysql

SELECT 
    contactname, IFNULL(bizphone, homephone) phone
FROM
    contacts;

Example 6: mysql if null

IFNULL(MAX(EMAIL), "[email protected]") as email

Tags:

Misc Example