SoFunction
Updated on 2025-03-03

Detailed explanation of judgment functions and branch statements in mysql

MySQL judges functions and branch statements

1. Determine the function

(1) if() function

  • if(expression, value 1, value 2) function:
  • It determines that the expression is successful, returns a value of 1, and fails to return a value of 2.

(2) ifnull() function

  • ifnull (column data, specify value) function:
  • It is judged that it is empty and returns the specified value, otherwise it will return to the original value of the column.

(3) nullif() function

  • nullif(expr1,expr2):
  • The same returns null, and the original value of expression 1 is retained differently.

2. Branch statements

(1) Multi-condition judgment

Case

        when condition1 then result1

        when condition1 then result1

        ...

        else result_n

end

Execution process:

  • Condition 1 is true and returns result 1, and it ends directly at this time.
  • Condition 1 is not true, condition 2 is implemented,
  • All conditions do not hold true and return the value of else.

(2) Fixed value judgment

Case List name

        when value1 then value1

        when value1 then value1

        ...

        else valuen

end

Execution process:

  • The value 1 is the same as the column value, and the return value 1 is returned, and it ends directly at this time.
  • Value 1 and column values ​​are different, continue to ask value 2 down...
  • All values ​​and column values ​​are different, and the value of else is returned.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.