mercredi 7 août 2019

if condition in where clause in MariaDB

These are the primary 4 columns in the EMP_TABLE that i'm using.

EMP_NAME EMP_ID EMP_DEPT JOIN_DATE

The general requirement is to fetch EMP_DEPT = 'HR' records only. But if EMP_DEPT = 'HR' records are NOT available in the table, then the requirement is to fetch EMP_DEPT = 'DEV' records from EMP_TABLE. The priority is to be given to HR records.

I know it can be achieved using inner queries or union and count. But I would like to know if it is possible using IF or WHEN. I've tried the following query and it doesn't seem to work.

select * from EMP_TABLE where if(EMP_DEPT='HR',EMP_DEPT='HR',EMP_DEPT='DEV');

Any help on this is appreciated.

Aucun commentaire:

Enregistrer un commentaire