Link to home
Create AccountLog in
Avatar of rcowen00
rcowen00Flag for United States of America

asked on

Correct Syntax for IFNULL MySQL

I am trying to put an if statement in a select query, but I'm getting a syntax error.  Any help appreciated.  Thank you.
$findings="SELECT * FROM loanFinding ,IFNULL(stSeverity,customSeverity) AS currSev "
    ."JOIN finding USING (findingKey) "
    ."WHERE loanFinding.active='1' AND loanFinding.auditID='$auditID' "
    ."ORDER BY currSev";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I normally prefer to explicitly list join columns, as well as where columns come from, e.g.

$findings="SELECT
    loanFinding.col1,
    loanFinding.col2,
    finding.colC,
    IFNULL(loanFinding.stSeverity,finding.customSeverity) AS currSev
FROM loanFinding
JOIN finding ON finding.findingKey = loanFinding.findingKey
WHERE loanFinding.active='1' AND loanFinding.auditID='$auditID'
ORDER BY loanFinding.currSev";

If I read correctly, is stSeverity the standard and customSeverity an optional override? Shouldn't the IFNULL be the other way around?
Also a hint that you can use multi-line strings in PHP, as shown above, instead of using the multiple . concats.