vendredi 5 juin 2020

Cannot handle right syntax for Mysql multiple else if query

Here is the query:

SELECT t1.id, 
(
    IF t1.sms_phone IS NOT NULL THEN t1.sms_phone
    ELSE IF t1.insuringPhone IS NOT NULL THEN t1.insuringPhone
    ELSE IF t1.person_id IS NOT NULL THEN (SELECT person_contact.value FROM person_contact where person_contact.person_id = t1.person_id AND person_contact.contact_id = 2 ORDER BY person_contact.last_change DESC LIMIT 1) END IF) as phone
FROM test_table as t1
WHERE id = 187842

Also tried with CASE without success:

SELECT t1.id, 
(
    CASE
    WHEN t1.sms_phone IS NOT NULL THEN t1.sms_phone
    WHEN t1.insuringPhone IS NOT NULL THEN t1.insuringPhone
    WHEN t1.person_id IS NOT NULL THEN (SELECT person_contact.value FROM person_contact where person_contact.person_id = t1.person_id AND person_contact.contact_id = 2 ORDER BY person_contact.last_change DESC LIMIT 1)
) as phone
FROM test_table as t1
WHERE id = 187842

Aucun commentaire:

Enregistrer un commentaire