Inserting values into column from another table in MySQL

时间:2015-07-31 20:40:16

标签: mysql database sql-update sql-insert

I am inserting one column values from one table to another table. I have following two tables.

 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

These both tables are having equal number of rows around 44300. ID and Entry_date columns are same for both the tables. I want to get insert all the values present in Table - b column TagsB to Table - a column TagsA. So the resulting table should look like this:

 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

I tried to use update:

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

How to proceed in this case?

1 个答案:

答案 0 :(得分:0)

try this one:

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