Sai Avinash Sai Avinash - 2 months ago 9
MySQL Question

SQL How to add if else conditions in sql

I have something like this

SELECT A.*, B.*
FROM TABLE1 A
INNER JOIN TABLE2 B ON A.MSISDN = B.MSISDN
WHERE A.FIRSTNAME != B.FIRSTNAME
OR A.LASTNAME != B.LASTNAME


How do i include an if else case here so that

if a.firstname == b.firstname && a.secondname == b.lastname - Ignore this record.

if a.firstname not equal to b.firstname - show this record as result

if a.firstname == b.firstname && a.secondname not equal to b.lastname - show this record as result


else show all the records as results that doesn't fall into any of these above cases.

thank you..

Answer

This should do the trick :

SELECT A.*, B.* 
FROM TABLE1 A
INNER JOIN TABLE2 B ON A.MSISDN = B.MSISDN
WHERE (A.FIRSTNAME,A.LASTNAME) NOT IN(B.FIRSTNAME,B.LASTNAME)

No need for IF , if this condition returns true, it means all your conditions are met, and either first_name is different or last_name is .