Search code examples
mysqlsqldatabaseconcat-ws

Concatenate the result of two concatenations in mySQL


I have this mySQL query:

SELECT 
CONCAT_WS('=>',column_1,column_2,column_3) 
AS column_union 
FROM table

In which the result is the combination of these 3 columns with => as delimiter.

table

Is it possible to concatenate in the same query the result of the first concatenation with any other columns?

For example:

SELECT CONCAT_WS('#**#',column_4,column_5,column_union) 
AS another_column_union 
FROM table

In which the final result another_column_union should look like this way:

value_column_4#**#value_column_5#**#v1=>va=>v0

Solution

  • You need to use a view, a subquery, or repeat the expression. It can also be simplified to:

    SELECT concat_ws('=>', column_1, column_2, column_3) as column_union,
           concat_ws('#**#', column_4, column_5, 
                     concat_ws('=>', column_1, column_2, column_3)
                    ) as another_column_union 
    FROM table