【问题标题】:How to transfer data from one column to another如何将数据从一列传输到另一列
【发布时间】:2021-11-05 03:21:21
【问题描述】:

我正在尝试将一列与另一列合并,但不确定应该使用什么插入或更新语句。

表格是这样的

marketId |    Product      | Phone | Value |
1          washing machine    null    800
1          air condition      null    300
1          refrigerator       null    600
1          TV                 null    500
2          washing machine    null    850
2          air condition      null    300
2          refrigerator       null    600
2          TV                 null    500

我想得到这样的结果 -

marketId |    Product      |  Value |
1          washing machine    800
1          air condition      300
1          refrigerator       600
1          TV                 500
1          Phone              null
2          washing machine    850
2          air condition      200
2          refrigerator       550
2          TV                 500
2          Phone              null

尝试了一些东西(更新/插入语句),但我没有成功。你还有其他想法吗? 谢谢!

【问题讨论】:

  • 请只标记您正在使用的数据库,SQL Server 还是 MySQL?
  • 手机总是空的吗?
  • 获取您发布的示例结果背后的逻辑是什么?

标签: sql sql-server database tsql


【解决方案1】:

你似乎想要:

select market_id, product, value
from t
union all
select market_id, 'phone', null
from t;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2022-11-17
    • 1970-01-01
    相关资源
    最近更新 更多