mysql连接字段

语法:

concat 等同于字符串连接符 ||,

CONCAT(字串1, 字串2, 字串3, ...): 将字串1、字串2、字串3,等字串连在一起。请注意,Oracle的CONCAT()只允许两个参数;换言之,一次只能将两个字串串连起来。

不过,在Oracle中,我们可以用'||'来一次串连多个字串。

例1:

concat(goods_sn,goods_title,goods_brief,goods_name) LIKE '%tablet%'

等价

goods_sn||goods_title||goods_brief||goods_name LIKE '%tablet%

ps:所有字段组合后进行模糊匹配

同时concat有to_char的作用,就是把其他类型转成varchar类型

 注意:select concat(A,B) from table ; 如果A和B其中一个为null, 那么最终合并起来还是空值

ps:碰到空值可以这样处理 select concat(A,ifnull(b,'')) from table;

实际案例:

假设我们有以下的表格:

Geography 表格

region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

例子1:

MySQL/Oracle
SELECT CONCAT(region_name,store_name) FROM Geography 
WHERE store_name = 'Boston';

结果

'EastBoston'

例子2:

Oracle
SELECT region_name || ' ' || store_name FROM Geography 
WHERE store_name = 'Boston';

结果

'East Boston'

例子3:

SQL Server
SELECT region_name + ' ' + store_name FROM Geography 
WHERE store_name = 'Boston';

原文地址:https://www.cnblogs.com/loveyouyou616/p/2780918.html