简体   繁体   中英

Mysql concat severals rows

Using Mysql and I am trying to concatenate several rows by using CONCAT_WS but I didn't get the desired output.

My table testtable has the following structure:

vardel1 int(50)
vardel2 int(50)
ComputeVariant varchar(50)

+----------+-------------------+----------------+
| vardel1d | vardel2           | ComputeVariant |
+----------+-------------------+----------------+
|  167     | 181               |  NULL          |
+----------+-------------------+----------------+

with my testtable I'm doing a join with another table ( posnucleo ) to compute values, 2235 and 2249 .

My desired output is in ComputeVariant column is:

c.2235_2249del

My sql query is the following:

update testtable, posnucleo
   set testtable.ComputeVariant = CONCAT_WS( CONCAT('c.', abs(((posnucleo.1stpos - posnucleo.1stnuclocode) - testtable.vardel1 )) ), CONCAT('_',abs(((posnucleo.1stpos - posnucleo.1stnuclocode) - testtable.vardel2 )),'del' ) )
 where testtable.Reference = posnucleo.amplicon

My issue is that I don't have the desired output. I also try to concat with || but it seems not working too. GROUP_CONCAT doesn't work either.

Do you have any idea how to resolve my issue ?

First, your query would be easier to read with explicit join syntax and table aliases.

I think you are overcomplicated the string concatenation. You can pass multiple arguments to concat() , like this:

update testtable tt join
       posnucleo pn
       on tt.Reference = pn.amplicon
    set tt.ComputeVariant = CONCAT('c.',
                                   abs((pn.1stpos - pn.1stnuclocode) - tt.vardel1 ) ),
                                   '_',
                                   abs((pn.1stpos - pn.1stnuclocode) - tt.vardel2 ),
                                   'del'
                                  );

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