问题:
将多列的值合并为一列。例如将 emp 表中的 ename,job 字段合并。
marry work as a manager
king work as a president
andy work as a clerk
例如原始数据为:
select ename,job from emp where deptno = '1'
上述sql结果集为:
ename
ename | job |
---|---|
marry | manager |
king | president |
andy | clerk |
解决方案:
(1)DB2、Oracle、PostgreSql:
这些数据库是用双竖线作为串联运算符。
select ename||' work as a '||job as msg from emp where deptno = '1'
(2)MySql:
使用concat 函数。
select concat(ename,' work as a ',job) as msg from emp where deptno = '1'
(3)sql server:
使用 “+” 作为串联。
select ename + ' work as a ' + job as msg from emp wher deptno = '1'