MySQL 连接运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15631137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:03:24  来源:igfitidea点击:

MySQL concatenation operator

mysqlsqlconcatenation

提问by user2201462

I don't know concatenation operator for MySQL.

我不知道 MySQL 的连接运算符。

I have tried this code for concatenation:

我已经尝试使用此代码进行连接:

SELECT vend_name || ' (' || vend_country || ')'
FROM Vendors
ORDER BY vend_name;

But it didn't work. Which operator should I use to concatenate strings?

但它没有用。我应该使用哪个运算符来连接字符串?

回答by 200_success

||is the ANSI standard string concatenation operator, supported by most databases (notably not MS SQL Server). MySQL also supports it, but you have to SET sql_mode='PIPES_AS_CONCAT';or SET sql_mode='ANSI';first.

||是 ANSI 标准字符串连接运算符,大多数数据库(尤其不是 MS SQL Server)都支持。MySQL 也支持它,但你必须SET sql_mode='PIPES_AS_CONCAT';SET sql_mode='ANSI';首先。

回答by codingbiz

You were using ORACLE type of concatenation. MySQL's Should be

您正在使用 ORACLE 类型的连接。MySQL的应该是

 SELECT CONCAT(vend_name, '(', vend_country, ')')

Call the CONCAT()function and separate your values with commas.

调用该CONCAT()函数并用逗号分隔您的值。

回答by Akshay Joy

MySQL CONCAT function is used to concatenate two strings to form a single string. Try out following example:

MySQL CONCAT 函数用于连接两个字符串以形成单个字符串。试试下面的例子:

mysql> SELECT CONCAT('FIRST ', 'SECOND');
+----------------------------+
| CONCAT('FIRST ', 'SECOND') |
+----------------------------+
| FIRST SECOND               |
+----------------------------+
1 row in set (0.00 sec)

To understand CONCAT function in more detail consider an employee_tbl table which is having following records:

要更详细地了解 CONCAT 函数,请考虑具有以下记录的 employee_tbl 表:

mysql> SELECT CONCAT(id, name, work_date)
    -> FROM employee_tbl;
+-----------------------------+
| CONCAT(id, name, work_date) |
+-----------------------------+
| 1John2007-01-24             |
| 2Ram2007-05-27              |
| 3Hyman2007-05-06             |
| 3Hyman2007-04-06             |
| 4Jill2007-04-06             |
| 5Zara2007-06-06             |
| 5Zara2007-02-06             |
+-----------------------------+

回答by T.S.

Whats good about using concatis that you can pass different data type columns and concat string representations

使用的好处concat是您可以传递不同的数据类型列和连接字符串表示

 SELECT concat('XXX',  10.99, 'YYY', 3, 'ZZZ', now(3)) as a; 

Output

输出

a
-----
XXX10.99YYY3ZZZ2018-09-21 15:20:25.106


-----
XXX10.99Y​​YY3ZZZ2018-09-21 15:20:25.106

回答by Kumar Anil Chaurasiya

Simply you can use CONCATkeyword to concatenate the Strings.. You can use it like

简单地你可以使用CONCAT关键字来连接字符串..你可以像这样使用它

SELECT CONCAT(vend_name,'',vend_country) FROM vendors ORER BY name;

回答by Xeno

You have to set pipes as concat every time before you run a query using pipes as a concatenate operator.

每次使用管道作为连接运算符运行查询之前,您都必须将管道设置为连接。