MySQL串联运算符?
您可以使用CONCAT()
MySQL的内置函数。语法如下-
SELECT CONCAT(('(',yourColumnName1,',', yourColumnName2,',',yourColumnName3,...N')')as anyVariableName from yourTableName;
为了理解上述语法,让我们首先创建一个表。创建表的查询如下-
mysql> create table Concatenation_OperatorDemo -> ( -> -> VendorId int, -> VendorName varchar(100), -> VendorCountry varchar(100) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into Concatenation_OperatorDemo values(101,'Carol','US'); mysql> insert into Concatenation_OperatorDemo values(102,'Bob','UK'); mysql> insert into Concatenation_OperatorDemo values(103,'John','US'); mysql> insert into Concatenation_OperatorDemo values(104,'David','UK');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from Concatenation_OperatorDemo;
以下是输出-
+----------+------------+---------------+ | VendorId | VendorName | VendorCountry | +----------+------------+---------------+ | 101 | Carol | US | | 102 | Bob | UK | | 103 | John | US | | 104 | David | UK | +----------+------------+---------------+ 4 rows in set (0.00 sec)
现在,您可以使用concat()
函数连接N个列。在这里,我们将合并上表中的所有三列。连接列值的查询如下-
mysql> select concat('(',VendorId,',', VendorName,',', VendorCountry,')')as ConcatenationDemo -> from Concatenation_OperatorDemo;
以下是输出-
+-------------------+ | ConcatenationDemo | +-------------------+ | (101,Carol,US) | | (102,Bob,UK) | | (103,John,US) | | (104,David,UK) | +-------------------+ 4 rows in set (0.00 sec)