SQL— CONCAT(字符串连接函数)

来源:互联网 发布:sql update多条数据 编辑:程序博客网 时间:2024/05/21 19:36

有的时候,我们有需要将由不同栏位获得的资料串连在一起。每一种资料库都有提供方法来达到这个目的:

  • MySQL: CONCAT()
  • Oracle: CONCAT(), ||
  • SQL Server: +

CONCAT() 的语法如下:

CONCAT(字串1, 字串2, 字串3,...): 将字串1、字串2、字串3,等字串连在一起。

请注意,Oracle的CONCAT()只允许两个参数;

换言之,一次只能将两个字串串连起来。不过,在Oracle中,我们可以用'||'来一次串连多个字串。

来看几个例子。假设我们有以下的表格:

Geography 表格

region_namestore_nameEastBostonEastNew YorkWestLos AngelesWestSan 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_nameFROM Geography
WHERE store_name = 'Boston';

结果

'East Boston'

分享:
0 0
原创粉丝点击