jonmrich
jonmrich

Reputation: 4323

Concat values into new column in mySQL

Here's the pseudo-code I'm trying to do:

update msa_list_copy 
select group_concat(distinct msa_name separator ', ') as concat_msa_name
group by msa

My table msa_list_copy has two columns...msa and msa_name. I'm trying to concatenate msa_names that share the same msa (number) and save this value in a (currently empty) third column `concat_msa_name, but I can't quite get it right.

Upvotes: 0

Views: 65

Answers (2)

sayani
sayani

Reputation: 148

The SQL is:

update msa_list_copy inner join  
(select group_concat(distinct msa_name separator ', ') as concat_msa_name
group by msa) as q on msa_list_copy.concat_msa_name=q.concat_msa_name set msa_list_copy.concat_msa_name = q.concat_msa_name

Upvotes: 0

Rasika
Rasika

Reputation: 1998

You need to specify the field you are updating.

update msa_list_copy join 
(select msa, group_concat(distinct msa_name separator ', ') 
      as concat_msa_name from msa_list_copy
   group by msa) as t0 using(msa) 
set msa_list_copy.concat_msa_name=t0.concat_msa_name

Upvotes: 1

Related Questions