I have a MySQL query where I would like it to have a pseudo column returned which is based off of:
Hopefully that makes sense. My SQL isn't good enough to wrap my head around this.
Try using a case expression:
SELECT CASE WHEN columnZ <> 0 THEN 4
WHEN columnY <> 0 THEN 3
WHEN columnX <> 0 THEN 2
WHEN columnW <> 0 THEN 1
ELSE 0
END AS your_alias_name
FROM ...
You can use CASE ... WHEN to do this.
See
mysql> SELECT CASE 1 WHEN 1 THEN 'one'
-> WHEN 2 THEN 'two' ELSE 'more' END;
-> 'one'
mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;
-> 'true'
mysql> SELECT CASE BINARY 'B'
-> WHEN 'a' THEN 1 WHEN 'b' THEN 2 END;
-> NULL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With