MYSQL SELECT WITHIN IF Statement MYSQL SELECT WITHIN IF Statement sql sql

MYSQL SELECT WITHIN IF Statement


How to use IF statement in select

select if(status>0, 'active', 'inactive') as status from users

Yes, you can use select within IF

Example:

select if((select count(*) from versions) > (select count(*) from groups), true, false) as value


Does it need to be within a procedure?

Yes, you do need to be in a procedure to use an if statement.

Can you use a SELECT within an IF statement?

Yes you can:

drop procedure if exists sp;create procedure sp () begin    if ((select 1 /*from whatever table would work*/)=1) then        select 'it works';    end if;end;call sp;drop procedure sp;


You can select another field from the same query but using select query might not be supportedas query can return multiple values , mysql not supporting row concept in a column.

SELECT IF(`field1`>1,`field2`,`field3`) FROM TABLE1 WHERE 1

here field1,field2,field3 are fields in a same table

Does this help you ?