4

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

2 Answers 2

8

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;
Sign up to request clarification or add additional context in comments.

Comments

4

You mean the IF(expr, expr, expr) function as defined here? An example would be:

SELECT
  name, ID,
  IF(category = 'fulltime', 1, 
    IF(category = 'parttime', loading, 0)) AS equivloading
FROM
  person

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.