MySQL concatenation operator

后端 未结 6 2187
长发绾君心
长发绾君心 2020-11-29 09:54

I don\'t know concatenation operator for MySQL.

I have tried this code for concatenation:

SELECT vend_name || \' (\' || vend_country || \')\'
FROM Ve         


        
相关标签:
6条回答
  • 2020-11-29 10:23

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

    0 讨论(0)
  • 2020-11-29 10:30

    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             |
    +-----------------------------+
    
    0 讨论(0)
  • 2020-11-29 10:34

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

    0 讨论(0)
  • 2020-11-29 10:35

    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;
    
    0 讨论(0)
  • 2020-11-29 10:41

    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.

    0 讨论(0)
  • 2020-11-29 10:43

    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

    0 讨论(0)
提交回复
热议问题