【问题标题】:Mysql concat severals rowsMysql concat 几行
【发布时间】:2016-05-09 22:41:36
【问题描述】:

使用Mysql,我尝试使用CONCAT_WS 连接几行,但没有得到所需的输出。

我的表testtable具有以下结构:

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

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

我正在使用我的 testtable 与另一个表 (posnucleo) 进行连接以计算值 22352249

我想要的输出在ComputeVariant 列是:

c.2235_2249del

我的sql查询如下:

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

我的问题是我没有想要的输出。我也尝试与|| 连接,但它似乎也不起作用。 GROUP_CONCAT 也不起作用。

您知道如何解决我的问题吗?

【问题讨论】:

    标签: mysql multiple-columns rows concat


    【解决方案1】:

    首先,使用明确的join 语法和表别名,您的查询会更容易阅读。

    我认为您的字符串连接过于复杂。您可以将多个参数传递给concat(),如下所示:

    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'
                                      );
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 2014-09-13
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多