mysql更新

bjg7j2ky  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(337)

下面的表格是最简单的形式。
表“pro\u details\u 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 |
+------+------------+--------+------------+

表“pro\u details\u 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)因此,我需要更新“问题”表中的“发布日期”列,同时比较上述02个表中的项目编号。已插入“问题”表的参考号和金额列。预期产出如下。

+----+--------+--------+-------------+
| 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)我使用了以下查询。

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)我不明白什么是错误的观点。有人能帮我吗?

guykilcj

guykilcj1#

使用join更新查询
选择要在其中更新的特定数据 issuestable 使用条件

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

如果要填充新数据表单,请插入查询 pro_details_old & pro_details_new 然后您可以使用下面的插入查询

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
    )

相关问题