Home > Database > Mysql Tutorial > body text

casewhen遇到空串转成0

WBOY
Release: 2016-06-07 15:59:26
Original
1234 people have browsed it

需要注意:如果字段为varchar类型,when后的条件要加上引号 SELECT (CASE marital_status WHEN 0 THEN 已婚 WHEN 1 THEN 未婚 ELSE WEIZHI END) AS marital_status FROM tj_archive WHERE id=D1407280006 用上面的语句,如果marital_status为空串(而不是null

需要注意:如果字段为varchar类型,when后的条件要加上引号
SELECT (CASE 'marital_status' WHEN 0 THEN '已婚' WHEN 1 THEN '未婚' ELSE 'WEIZHI' END) AS marital_status  FROM tj_archive WHERE id='D1407280006'
Copy after login

用上面的语句,如果marital_status为空串(而不是null)时,竟然查询的结果为“已婚”

最后更改正确结果为:

SELECT (CASE 'marital_status' WHEN '0' THEN '已婚' WHEN '1' THEN '未婚' ELSE 'WEIZHI' END)AS marital_status FROM tj_archive WHERE id='D1407280006'

红色部分为两句的不同。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!