mysql case when使用
时间: 2023-10-19 11:31:24 浏览: 102
在MySQL中,CASE WHEN语句用于根据条件进行条件判断和结果返回。它的基本语法结构是:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE resultN
END
在这个语法结构中,你可以根据需要添加任意多个WHEN条件和结果。当满足某个WHEN条件时,就会返回对应的结果。如果没有满足任何条件,就会返回ELSE后面的结果。
例如,使用简单CASE函数在SELECT语句中进行条件判断,可以写成:
SELECT CASE parent_id
WHEN 0 THEN '00'
WHEN 1 THEN '11'
ELSE 'OTHERS'
END AS parent_id_new, parent_id, type_id, type_name
FROM tdb_goods_types
这个例子中,根据parent_id的值进行条件判断,如果parent_id为0,则返回'00',如果parent_id为1,则返回'11',否则返回'OTHERS'。同时,查询结果中还包括了parent_id、type_id和type_name这些列。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [MySql中case when的用法](https://blog.csdn.net/m0_60741007/article/details/128707763)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [mysql中case when的用法](https://blog.csdn.net/u010147215/article/details/124529659)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文