if else query in mysql

自古美人都是妖i 提交于 2020-01-23 01:29:10

问题


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


回答1:


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;



回答2:


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


来源:https://stackoverflow.com/questions/3059136/if-else-query-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!