gpt4 book ai didi

sql - sql 中的 concat() 如何添加两个或多个参数,包括特殊字符?

转载 作者:行者123 更新时间:2023-12-01 11:20:06 26 4
gpt4 key购买 nike

select CONCAT(address,city) as Address from student order by Address desc;

这是一个查询,用于显示学生表中别名为地址的地址和城市

--- Program output ---

ADDRESS
----------------------------------------------------------------------
LMCCoimbatore
FFFVilupuram
BBBAgra
ABCSalem
AAAPondichery


--- Expected output (text)---
ADDRESS
------------------------------------------------------------------------
LMC, Coimbatore
FFF, Vilupuram
BBB, Agra
ABC, Salem
AAA, Pondichery

如何在城市和地址之间添加昏迷和空格?当我将第三个字符串用作“,”时,它给出了无效的参数数量错误。

最佳答案

CONCAT 版本:

我会推荐 CONCAT,因为它会保护您在其中一个参数为 null 时不返回 NULL - 至少在 MSSQL 中是这样。

MSSQL & MySQL & PostgresSQL & Teradata:

select CONCAT(address,', ',city) as Address from student order by Address desc;

MySQL 版本 2:

select CONCAT_WS(', ', address,city) as Address from student order by Address desc;

甲骨文、IBM DB2 和 IBM Informix:

select CONCAT(address,CONCAT(', ',city)) as Address from student order by Address desc;

IBM DB2:

select address CONCAT ', ' CONCAT city as Address from student order by Address desc;

非 CONCAT 版本:

||是 ANSI 标准,但并非所有系统都使用它。

MSSQL 和 Sybase IQ:

select address + ', ' + city as Address from student order by Address desc;

MySQL(所谓的邻近运算符,默认行为):

select address ', ' city as Address from student order by Address desc;

PostgresSQL & Oracle & MySQL(使用 SET sql_mode='PIPES_AS_CONCAT';)& IBM Informix & Sybase IQ:

select address || ', ' || city as Address from student order by Address desc;

关于sql - sql 中的 concat() 如何添加两个或多个参数,包括特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44878718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com