mysql多条件替换

[复制链接]
查看11 | 回复3 | 2010-10-25 16:43:45 | 显示全部楼层 |阅读模式
数据库表1内有两个字段.分别为a字段和B字段.当A字段里面有内容的时候b字段内容为1,当A字段里面没有内容的时候,B字段内容为0 ,请教大家帮帮忙.如何处理
SELECT A,(case when (A is null) or (A='') then 0 else 1 end) as B
FROM t_test;
能查询,但不能执行

回复

使用道具 举报

千问 | 2010-10-25 16:43:45 | 显示全部楼层
SELECT A,(case when (A is null) or (A='') then 0 else 1 end) as B
FROM t_test;说明:
t_test是数据表名,B是个不存在的字段,只是一个输出结果,所以真实的字段只有字段A。以上sql语句在mysql5.1上测试通过。
回复

使用道具 举报

千问 | 2010-10-25 16:43:45 | 显示全部楼层
update table1 set b=1 where a is not null or a'';update table1 set b=0 where a is null or a'';执行两遍比较好
回复

使用道具 举报

千问 | 2010-10-25 16:43:45 | 显示全部楼层
update table1 a set b=(select case when a is null then 0 else 1 end from table1 b where a.id=b.id);这要求表1有个主键字段id;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行