Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql SELECT IF statement with OR

The following works - returns Y when chargeback equal to 1 else it defaults to N

IF(fd.charge_back = 1, 'Y', 'N') AS charge_back 

however I cannot seem to get this one working? Is the syntax valid

IF(compliment = ('set' OR 'Y' OR 1), 'Y', 'N') AS customer_compliment 
like image 619
Robbo_UK Avatar asked Feb 02 '12 12:02

Robbo_UK


People also ask

Can you use && in MySQL?

The && , operator is a nonstandard MySQL extension. As of MySQL 8.0. 17, this operator is deprecated; expect support for it to be removed in a future version of MySQL. Applications should be adjusted to use the standard SQL AND operator.

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.


2 Answers

Presumably this would work:

IF(compliment = 'set' OR compliment = 'Y' OR compliment = 1, 'Y', 'N') AS customer_compliment 
like image 176
Digbyswift Avatar answered Oct 14 '22 11:10

Digbyswift


IF(compliment IN('set','Y',1), 'Y', 'N') AS customer_compliment 

Will do the job as Buttle Butkus suggested.

like image 27
AleXqwq Avatar answered Oct 14 '22 09:10

AleXqwq