MySQL 的连接操作符是什么?
您可以使用 MySQL 中内置函数 CONCAT()。语法如下所示 -
SELECT CONCAT(('(',yourColumnName1,',', yourColumnName2,',',yourColumnName3,...N')')as anyVariableName from yourTableName;
为了理解上述语法,让我们首先创建一个表。创建表的查询如下 -
mysql> create table Concatenation_OperatorDemo -> ( -> -> VendorId int, -> VendorName varchar(100), -> VendorCountry varchar(100) -> ); Query OK, 0 rows affected (0.69 sec)
使用 insert 命令在表中插入一些记录。查询如下 -
mysql> insert into Concatenation_OperatorDemo values(101,'Carol','US'); Query OK, 1 row affected (0.19 sec) mysql> insert into Concatenation_OperatorDemo values(102,'Bob','UK'); Query OK, 1 row affected (0.18 sec) mysql> insert into Concatenation_OperatorDemo values(103,'John','US'); Query OK, 1 row affected (0.14 sec) mysql> insert into Concatenation_OperatorDemo values(104,'David','UK'); Query OK, 1 row affected (0.25 sec)
使用 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() 函数连接任意数量的列。在此,我们将连接上述表中的所有三列。连接列值的查询如下 -
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)
广告