【问题标题】:SQL copy data from one column to another table specified columnSQL 将数据从一列复制到另一表指定列
【发布时间】:2012-11-03 17:07:32
【问题描述】:

我有两个名为 orderorder_product 的表名,两个表都有一个同名的列 model, order_product model 列有很多数据,但 order model 为空字段。

我想将表order_product 中的model 数据复制到modelorder,我该怎么做?

我尝试了一些 SQL 查询,但结果并不像我想要的那样,它看起来所有字段都会重复...

INSERT INTO `order` (model) SELECT (model) FROM `order_product`

【问题讨论】:

  • 我只想将数据从表 order_product 字段模型插入到表 order 字段模型中...
  • 当我尝试上面的代码时,结果看看我有多少数据字段将被复制。

标签: php mysql sql database


【解决方案1】:

尝试使用DISTINCT 来消除SELECT 子句中的重复行,如下所示:

INSERT INTO `order` (model) 
SELECT DISTINCT model FROM `order_product`;

SQL Fiddle Demo

【讨论】:

    【解决方案2】:
    INSERT INTO order (model)
    SELECT model FROM order_product
    WHERE 'some field' = (some condition)
    

    【讨论】:

      【解决方案3】:
      INSERT INTO table1 ( column1 )
      SELECT  col1
      FROM    table2
      

      这应该适用于你的问题?请让我知道你想要的输出是什么,除非我会更新答案

      看到你的cmets

      INSERT INTO table1 ( column1 )
      SELECT distinct(col1)
      FROM    table2
      

      【讨论】:

      • 我尝试过您的答案,例如 INSERT INTO order (model)SELECT distinct(model) FROM order_product 但仍然与上述问题相同
      • 比如你能告诉我一些重复的示例输出
      • 我的表有近 20 个字段,所有字段数据都重复但为空。只有模型字段有我们从其他表复制的数据
      • 如果您运行此查询,您将只从该表中获取模型的数据,其他字段将为空或仅空???你想要数据为空的字段是什么
      • @mans 你想更新列模型还是插入列模型?
      猜你喜欢
      • 2020-11-03
      • 1970-01-01
      • 2015-01-28
      • 2016-03-15
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2017-03-28
      相关资源
      最近更新 更多