Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if else query in mysql

Tags:

mysql

i need an example of nested if-else condition in mysql query

like image 868
user222585 Avatar asked Dec 17 '22 00:12

user222585


1 Answers

You could also use case statements for if-else conditions

SELECT
  (CASE field1
    WHEN 'A' THEN 'value is A'
    WHEN 'B' THEN 'value is B'
    ELSE 'value is neither A or B'
  END)
FROM your_table;

or

SELECT
  (CASE
    WHEN (field1 IS NULL) THEN 'value is NULL'
    WHEN (field1 = 1) THEN 'value is 1'
    ELSE 'value is neither NULL or 1'
  END)
FROM your_table;
like image 65
Ivar Bonsaksen Avatar answered Jan 28 '23 04:01

Ivar Bonsaksen