MySQL concatenation operator

痞子三分冷 提交于 2019-12-17 06:49:29

问题


I don't know concatenation operator for 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?


回答1:


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

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

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




回答2:


|| 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.




回答3:


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

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:

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



回答4:


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

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



回答5:


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

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

Output

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




回答6:


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



来源:https://stackoverflow.com/questions/15631137/mysql-concatenation-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!