简体   繁体   中英

Inserting values into column from another table in MySQL

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?

try this one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM