mjemerson
mjemerson

Reputation: 55

Having trouble with an IFNULL in a mySQL WHERE clause

Before anyone says, I have searched through for a suitable answer for my issue but cannot find anything specific enough so I thought I'd ask it.

Basically I am trying to select a bunch of data for a report of people who have made loan applications to a website, but there are two different types: unsecured and guarantee. I need to place an IFNULL statement in the WHERE clause so that I ONLY use that clause if a certain other field isn't null.

Here is my statement:

SELECT 
    la.`lms_loan_application_id`,
    la.`created`,
    la.`updated`,
    la.`loan_amount`,
    la.`loan_term`,
    la.`loan_document_fee`,
    la.`broker_reference`,
    la.`broker_sub_reference`,
    laa.`first_name`,
    laa.`surname`,
    laa.`dob`,
    laa.`email`,
    laa.`mobile_number`,
    laaAd.`address_postcode`,
    lag.`first_name`,
    lag.`surname`,
    lag.`dob`,
    lag.`email`,
    lag.`mobile_number`,
    lagAd.`address_postcode`,
    lagAd.`housing_status`
FROM
    loan_application AS la
        JOIN
    loan_application_applicant AS laa ON la.`id` = laa.`loan_application`
        LEFT JOIN
    loan_application_guarantor AS lag ON la.`id` = lag.`loan_application`
        JOIN
    loan_application_address AS laaAd ON laaAd.`loan_application_applicant` = laa.`id`
        LEFT JOIN
    loan_application_address AS lagAd ON lagAd.`loan_application_guarantor` = lag.`id`
WHERE
    la.`status` = 'signature_given'
        AND ! IFNULL(lag.`first_name`,
            lag.`status` = 'signature_given')
        AND laa.`status` = 'signature_given'
        AND ! IFNULL(lag.`first_name`,
            lagAd.`current_address` = 1)
        AND laaAd.`current_address` = 1
ORDER BY la.`updated` DESC
LIMIT 10000

As you can see, I have attempted to use the IFNULLs (although in a negated way, which I assume works?) but all I get is duplicate row results and not the result set I really want.

Basically, I need to use the where clause "lag.status = 'signature_given" and "lagAd.current_address = 1" ONLY if the lag.first_name field is NOT null (i.e. there is a guarantor name) otherwise the status won't exist, and therefore the results of unsecured loans will not show. Hope I'm explaining this well enough!

In summary, I need to show all loan information, unsecured and guaranteed, and use a negated IFNULL in order to determine when the WHERE clause is to be taken into consideration.

Any help appreciated!

Thank you in advance Michael

Upvotes: 1

Views: 693

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521073

From this MySQLTutorial article:

Notice that you should avoid using the IFNULL function in the WHERE clause, because it degrades the performance of the query. If you want to check if a value is NULL or not, you can use IS NULL or IS NOT NULL in the WHERE clause.

Here is a WHERE clause which implements your logic correctly using IS NULL and IS NOT NULL instead of IFNULL:

WHERE la.`status` = 'signature_given' AND
(lag.`first_name` IS NULL OR
(lag.`first_name` IS NOT NULL AND lag.`status` = 'signature_given')) AND
laa.`status` = 'signature_given' AND
(lag.`first_name` IS NULL OR
(lag.`first_name` IS NOT NULL AND lagAd.`current_address` = 1)) AND 
laaAd.`current_address` = 1

Upvotes: 1

Related Questions