Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A MySQL IF condition

In my query I have an IF statement:

IF(friend.block, 'yes', 'no') 

where friend.block value is 0 or 1.. but either way its putting 'yes'.. any idea?

like image 928
Basit Avatar asked Dec 16 '25 15:12

Basit


2 Answers

friend.block should be of type INTEGER for that to work or you have to put a comparaison there:

IF(friend.block != 0, 'yes', 'no')
like image 79
manji Avatar answered Dec 19 '25 05:12

manji


Reference: MySQL Reference

The syntax is:

IF (friend.block = 1) THEN
    'Yes'
ELSE
    'No'
END IF

You can use case statement:

CASE friend.block WHEN 1 THEN 'Yes' WHEN 0 THEN 'No' END

like image 42
Kirtan Avatar answered Dec 19 '25 05:12

Kirtan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!