简体   繁体   中英

MySQL Update with Select Join

I have following tables as the simplest form.

Table "pro_details_old"

+------+------------+--------+------------+
|  id  | project_no | amount |  pro_date  |
+------+------------+--------+------------+
| 1000 | 001/001    |  50000 | 2018-10-01 |
| 1001 | 001/002    |  25000 | 2018-10-06 |
| 1002 | 002/004    |  75000 | 2018-10-12 |
| 1003 | 002/005    |  65000 | 2018-09-22 |
| 1004 | 002/006    |  10000 | 2018-08-17 |
| 1005 | 003/002    |  12000 | 2018-10-08 |
| 1006 | 003/003    | 145000 | 2018-07-01 |
+------+------------+--------+------------+

Table "pro_details_new"

+------+------------+--------+----------+
|  id  | project_no | amount | pro_date |
+------+------------+--------+----------+
| 1050 | 001/001    |  50000 |          |
| 1051 | 001/002    |  25000 |          |
| 1052 | 002/004    |  75000 |          |
| 1053 | 002/005    |  65000 |          |
| 1054 | 002/006    |  10000 |          |
| 1055 | 003/002    |  12000 |          |
| 1056 | 003/003    | 145000 |          |
+------+------------+--------+----------+

02) So, I need to update issued_date column in "issues" table while comparing project_no of above 02 tables. ref_no & amount columns of "issues" table are already inserted. Expected output as follows.

+----+--------+--------+-------------+
| id | ref_no | amount | issued_date |
+----+--------+--------+-------------+
|  1 |   1050 |  50000 | 2018-10-01  |
|  2 |   1051 |  25000 | 2018-10-06  |
|  3 |   1052 |  75000 | 2018-10-12  |
|  4 |   1053 |  65000 | 2018-09-22  |
|  5 |   1054 |  10000 | 2018-08-17  |
|  6 |   1055 |  12000 | 2018-10-08  |
|  7 |   1056 | 145000 | 2018-07-01  |
+----+--------+--------+-------------+

03) I used the following query.

insert into issues 
set issued_date = 
      (select pro_date 
       from pro_details_old 
       where 
          pro_details_old.project_no = pro_details_new.project_no) 
       left join pro_details_new on pro_details_new.id = issues.ref_no

04) I can not understand what is the wrong point. Can anyone help me ?

Update Query using join

Select the particular data which you want to update in issuestable using condition

update 
    issuestable as t3 
    join (
        select 
            t1.id, 
            t1.pro_date 
        from 
            pro_details_old as t1 
            inner join pro_details_new as t2 on t1.project_no = t2.project_no
    ) t4 on t4.id = t3.ref_no 
set 
    t3.issued_date = t4.pro_date 

Insert Query If you want to populate fresh data form pro_details_old & pro_details_new then you can use below insert query

INSERT INTO issues (ref_no, amount, issued_date) 
VALUES 
    (
        select 
            t1.id as ref_no, 
            t1.amount, 
            t1.pro_date as issued_date 
        from 
            pro_details_old as t1 
            inner join pro_details_new as t2 on t1.project_no = t2.project_no
    )

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