mysql特定语句-----多行数据合并成一行,实现oracle中的decode函数效果

1、 mysql中将多行数据合并成一行数据
使用group_concat函数

select id, group_concat(list_name) as listnames from list

2、 在mysql中不存在decode函数用法,可以用以下方式替换

// oracle
select decode(ttype, 1, 'a', 2, 'b', 3, 'c', 'd') from taba;

// mysql
select if (ttype=1, 'a', if (ttype=2, 'b', if(ttype=3, 'c', 'd'))) from taba;
select case ttype when 1 then 'a' when 2 then 'b' when 3 then 'c' else 'd' end from taba;

猜你喜欢

转载自blog.csdn.net/miracle_8/article/details/80290886