下面的 sql 语句是要将 status
列根据一个条件或者多条件转换为对应的值. 其中要注意 case
关键字后面不能带上列名 status
而是直接跟上 when
关键词, 不然会导致转换无效.
SELECT id, case
WHEN cast(`status` AS SIGNED) < 45 THEN '1'
WHEN cast(`status` AS SIGNED) > 44 AND vacant_time IS NOT NULL AND vacant_time != '' THEN '3'
WHEN cast(`status` AS SIGNED) > 44 AND move_date IS NOT NULL AND move_date != '' THEN '2'
WHEN cast(`status` AS SIGNED) > 44 THEN '4'
ELSE '99'
END AS `status`
FROM t_household WHERE del_flag = '0';