【问题标题】:Merge two columns in MySQL and ignore the NULL合并 MySQL 中的两列并忽略 NULL
【发布时间】:2019-01-21 04:46:41
【问题描述】:

MySQL数据表如下:

Column1   Column2
A         NULL
B         NULL
NULL      C
NULL      D
NULL      NULL

需要输出如下:

Column3
A
B
C
D
NULL

我怎样才能得到这种合并?
最好使用 MySQL 查询。
如果最后没有可用的选项,那么我可以寻找 python 代码。

【问题讨论】:

  • 参见 COALESCE()。
  • @Strawberry 真的很有帮助。谢谢。

标签: mysql mysql-workbench mysql-python


【解决方案1】:
select COALESCE(Column1, Column2) as Column3 from tablename

这是有效的。

【讨论】:

    【解决方案2】:

    您需要在 mysql 中连接公共列和用例上的表。 您可以执行以下操作..

    CREATE TABLE new_tbl [AS] SELECT CASE
        WHEN orig_tbl1.col1 IS NOT NULL THEN orig_tbl1.col1
        WHEN orig_tbl1.col2 IS NOT NULL THEN orig_tbl1.col2
        ELSE NULL
        END , orig_tbl1.col2,orig_tbl1.col3,
     FROM orig_tbl1,orig_tbl2 where orig_tbl1.id = orig_tbl2.id ;
    

    你可以试试这个链接。 https://stackoverflow.com/a/8600850/1069633

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 2017-08-05
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      相关资源
      最近更新 更多