我有以下表格是最简单的形式。
表“ 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 |
+------+------------+--------+------------+
表“ 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)因此,我需要在比较上述02个表的project_no时更新“问题”表中的issue_date列。 “问题”表的ref_no和数量列已插入。预期输出如下。
+----+--------+--------+-------------+
| 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)我不明白什么是错点。谁能帮我 ?
答案 0 :(得分:0)
使用联接更新查询
使用条件选择要在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
)