【问题标题】:Inserting values into column from another table in MySQL将值插入 MySQL 中另一个表的列中
【发布时间】:2015-07-31 20:40:16
【问题描述】:

我正在将一个表中的一列值插入到另一个表中。我有以下两张表。

 Table - a

    ID    Entry_date    weight   height    TagsA
    111   1968-07-31    22       34
    111   1968-12-31    34       37
    112   1969-03-31    8        43
    112   1969-07-31    45       48
    113   1970-09-30    67       94
    113   1973-03-31    23       76

   Table - b

    ID    Entry_date    TagsB
    111   1968-07-31    1
    111   1968-12-31    1
    112   1969-03-31    0
    112   1969-07-31    0
    113   1970-09-30    0
    113   1973-03-31    1

这两个表的行数相同,大约为 44300。两个表的 ID 和 Entry_date 列相同。我想将表中存在的所有值-b 列TagsB 插入到表-a 列TagsA。所以结果表应该是这样的:

 Table - a

    ID     Entry_date    weight   height    TagsA
    111   1968-07-31    22       34        1
    111   1968-12-31    34       37        1
    112   1969-03-31    8        43        0
    112   1969-07-31    45       48        0
    113   1970-09-30    67       94        0
    113   1973-03-31    23       76        1

我尝试使用更新:

update a set TagsA = (select TagsB from b where a.ID = b.ID and a.Entry_date = b.Entry_date);

Error Code: 1242. Subquery returns more than 1 row  714.523 sec

这种情况如何处理?

【问题讨论】:

  • update 支持连接,所以update a set a.tags=b.tags join ...

标签: mysql database sql-update sql-insert


【解决方案1】:

试试这个:

update a set TagsA = (select max(TagsB)
    from b
    where a.ID = b.ID and a.Entry_date = b.Entry_date);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-27
    • 2015-03-21
    • 2023-03-29
    • 2015-05-28
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多