Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL SELECT WITHIN IF Statement

Tags:

This is such a simple question but I haven't found an answer anywhere for 2 hours.

How do you use the MYSQL IF statment. No matter what I put in it doesn't work. In SQL Server this is a 5 second job.

Does it need to be within a procedure?

Can you use a SELECT within an IF statement?

Not really interested in the IF function here.

Thanks for any help.

like image 700
tura Avatar asked Jan 23 '11 16:01

tura


People also ask

Can we use if statement in select query in MySQL?

Answer: MySQL IF() function can be used within a query, while the IF-ELSE conditional statement construct is supported to be used through FUNCTIONS or STORED PROCEDURES.

How do I select a condition in MySQL?

You can specify any condition using the WHERE clause. You can specify more than one condition using the AND or the OR operators. A WHERE clause can be used along with DELETE or UPDATE SQL command also to specify a condition.

Can we use if statement in select query in SQL?

Both IIF() and CASE resolve as expressions within a SQL statement and can only be used in well-defined places. The CASE expression cannot be used to control the flow of execution of Transact-SQL statements, statement blocks, user-defined functions, and stored procedures.


1 Answers

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 
like image 158
Srihari Goud Avatar answered Mar 16 '23 00:03

Srihari Goud