Mysql之字段字符串拼接

需求:

mysql的字符串拼接

实现:

concat(str1,str2,str3)

concat_ws(separator,str1,str2,str3)

实现中只需要注意null的问题,说明如下:

concat()中只要有一个字符串为null那么返回值为null

concat_ws():

1.separator为null时返回为null

2.其他字符串为null的时候直接跳过

例子1:

select concat(id,xf,null) from data limit 1;

值为:

写道
+--------------------+
| concat(id,xf,null) |
+--------------------+
| NULL |

例子2:

select concat_ws('=',id,xf,null) from data limit 1;

返回值:

写道
+------------------------------------+
| concat_ws('=',id,xf,null) |
+------------------------------------+
| 070e997f147c93113b7e522e9f355dc0=2 |

例子3:

select concat_ws(null,id,xf) from data limit 1;

返回值:

写道
+-----------------------+
| concat_ws(null,id,xf) |
+-----------------------+
| NULL |

 

猜你喜欢

转载自snv.iteye.com/blog/2234929