Ahmed Samy Ahmed Samy - 1 month ago 9
MySQL Question

MySQL Concatenate last item by dot

i have table Orders

i want to

select (OrderName) Values Concatenated by (dash) While last OrderName Concatenated by (Dot)


i used the following :
SELECT CONCAT(OrderName , ' - ') AS 'Name' FROM Orders


Example :

order1 - order2 - order3 .

Answer

Just use GROUP_CONCAT

 SELECT CONCAT ( SUBSTRING(`Name`, 1, CHAR_LENGTH(`Name`) - 2),
                 ' . ') as `Name`
 FROM (
           SELECT GROUP_CONCAT(OrderName, ' - ') AS `Name`
           FROM Orders
      ) T
Comments