if else在mysql中查询

时间:2010-06-17 05:37:54

标签: mysql

我需要在mysql查询中嵌套if-else条件的示例

2 个答案:

答案 0 :(得分:8)

您还可以使用if-else条件的案例陈述

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;

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;

答案 1 :(得分:4)

您的意思是IF( expr expr expr 功能defined here?一个例子是:

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