更新table1的列,在table2上获取
Update the column of table1 and get it on table2
我如何创建一个查询来更新我在 table2 上获得的 table1 列日期?
这是 tables
的一些示例
表 1:
| stud_id | start_date | birt_date | name | exam_date |
| s001 | 11/19/2018 | 05/20/1999 | john | 10/20/2018 |
| s003 | 01/01/2018 | 05/25/1995 | mike | 10/20/2018 |
| s005 | 12/23/2018 | 02/20/1999 | ed | 10/20/2018 |
| s005 | 12/23/2018 | 02/20/1999 | ed | 10/05/2017 |
表 2:
| stud_id | start_date | exam_date |
| s005 | 01/01/2017 | 10/20/2018 |
| s001 | 01/01/2017 | 10/20/2018 |
| s003 | 01/01/2017 | 10/20/2018 |
基本上我只想更改 3 的 start_date
,因此 s006
不会更改。
我怎样才能使用查询来完成它?我当时想在 select 中使用 table,但我认为它不会起作用。
我需要基于两列或更多列来更新我的条件,所以我想更新 table 1 其中 table1.stud_id
= table2.stud_id
和 table1.exam_date
= table2.exam_date
加入并更新
update t1
set t1.stardate=t2.startdate,
t1.exam_date=t2.exam_date
from table1 t1 join table2 t2
on t1.stud_id=t2.stud_id
where t2.stud_id='s003' -- if you just need s003 update
您也可以使用 CTE 来更新:
;With cte as
(
select t1.start_date as t1date,t2.start_date as t2date from table1 t1
join table2 t2
on t1.stud_id=t2.stud_id
and t1.exam_date=t2.exam_date
)
update cte set t1date=t2date
您可以加入 2 个表:
UPDATE T1
SET Start_Date = T2.Start_Date
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.stud_id = T2.stud_id
AND T1.exam_date = T2.exam_date
我如何创建一个查询来更新我在 table2 上获得的 table1 列日期?
这是 tables
的一些示例表 1:
| stud_id | start_date | birt_date | name | exam_date |
| s001 | 11/19/2018 | 05/20/1999 | john | 10/20/2018 |
| s003 | 01/01/2018 | 05/25/1995 | mike | 10/20/2018 |
| s005 | 12/23/2018 | 02/20/1999 | ed | 10/20/2018 |
| s005 | 12/23/2018 | 02/20/1999 | ed | 10/05/2017 |
表 2:
| stud_id | start_date | exam_date |
| s005 | 01/01/2017 | 10/20/2018 |
| s001 | 01/01/2017 | 10/20/2018 |
| s003 | 01/01/2017 | 10/20/2018 |
基本上我只想更改 3 的 start_date
,因此 s006
不会更改。
我怎样才能使用查询来完成它?我当时想在 select 中使用 table,但我认为它不会起作用。
我需要基于两列或更多列来更新我的条件,所以我想更新 table 1 其中 table1.stud_id
= table2.stud_id
和 table1.exam_date
= table2.exam_date
加入并更新
update t1
set t1.stardate=t2.startdate,
t1.exam_date=t2.exam_date
from table1 t1 join table2 t2
on t1.stud_id=t2.stud_id
where t2.stud_id='s003' -- if you just need s003 update
您也可以使用 CTE 来更新:
;With cte as
(
select t1.start_date as t1date,t2.start_date as t2date from table1 t1
join table2 t2
on t1.stud_id=t2.stud_id
and t1.exam_date=t2.exam_date
)
update cte set t1date=t2date
您可以加入 2 个表:
UPDATE T1
SET Start_Date = T2.Start_Date
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.stud_id = T2.stud_id
AND T1.exam_date = T2.exam_date