一、mysql
在java中我们通常用加号+来实现字符串的拼接,mysql中也可以使用+来实现,比如:先加入测试数据
create table test( id int, name varchar(10), score float );insert into test values(1,'zhang',98);insert into test values(2,'li',95);
select name+'hello' from test;
执行结果:demo2
select score,score+5 from test;
执行结果:
concat函数支持一个或者多个参数,参数类型可以为字符串类型也可以是非字符串类型,对于非字符串类型的参数mysql将尝试
将其转化为字符串类型,concat函数会将所有参数按照参数的顺序拼接成一个字符串做为返回值。
select concat(name,'-hello',' good') from test;
执行结果:
mysql中还提供了另外一个进行字符串拼接的函数concat_ws,
concat_ws可以在待拼接的字符串之间加入指定的分隔符,第一个参数为要设置的分隔符,
而剩下的参数则为待拼接的字符串值
select concat_ws('-',name,'考了',score) from test;
执行结果:
二、oracle
oracle中使用||进行字符串拼接
select name||'hello' from test;
执行结果:
除了||,oracle还支持使用concat()函数进行字符串拼接
select concat(name,score) from test;
执行结果:
如果concat中连接的值不是字符串,oracle会尝试将其转换为字符串,
与mysql的concat()函数不同,oracle的concat()函数只支持两个参数,不支持两个以上字符串的拼接。