简体   繁体   中英

MySQL: variable = variable + select statement

I've got several records in a temp table, and I'd like to take a value from each record and add it to a variable, eg

color          | caption
--------------------------------
red              stop
blue             state line
yellow           yield
orange           construction

var1 = 'red;blue;yellow;orange'

In MSSQL, I could do that this way:

-- MSSQL
select var1 = var1 + color
from signShapes

In MySQL, that doesn't work. I'd just get "orange."

-- mySQL
select var1 = concat(var1, color)
from signShapes

No, I know I could accomplish this with a cursor or a loop. I'm curious if I could do this without either, similarly to the MSSQL method.

 SELECT var1 = GROUP_CONCAT(color SEPARATOR ';')
 FROM signShapes
 GROUP BY color;

Why not just do this?

select var1 = group_concat(color separator ';')
from signShapes;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM