我正在尝试将对应的毕业日期值从毕业术语表复制到 user_education_mba_school 表中具有匹配的毕业术语 ID 的行中。这是我的非工作查询
TABLE DEFS
**user_education_mba_school
school_id
graduation_date
graduation_term_id
**graduation_term
graduation_term_id
graduation_year_id
graduation_date
**graduation_class
graduation_class_id
graduation_year_id
**user
user_id
graduation_class_id
-- 多重连接不起作用...
UPDATE u
SET graduation_class_id = gc.graduation_class_id
FROM [user] u
JOIN user_education_mba_school mba
ON mba.user_id = u.user_id
JOIN graduation_term gt
ON mba.graduation_term_id = gt.graduation_term_id
JOIN graduation_class gc
ON gt.graduation_year_id = gc.graduation_year_id
一些数据库支持
update ... from
语法,这是相当清晰的:
UPDATE mba
SET graduation_date = gt.graduation_date
FROM user_education_mba_school mba
JOIN graduation_term gt
ON gt.graduation_term_id = mba.graduation_term_id
如果您的数据库没有,请说明您正在使用哪个数据库。
这不起作用,因为您正在设置子查询的整个结果集
UPDATE user_education_mba_school SET graduation_date = (
SELECT gt.graduation_date
FROM user_education_mba_school mba, graduation_term gt
WHERE gt.graduation_term_id = mba.graduation_term_id
)
这应该有效(未经测试)
UPDATE user_education_mba_school mba SET graduation_date = (
SELECT gt.graduation_date
FROM graduation_term gt
WHERE gt.graduation_term_id = mba.graduation_term_id
)
UPDATE mba
SET graduation_date = gt.graduation_date, mba_graduation_year_id = gt.graduation_year_id
FROM user_education_mba_school mba
JOIN [sueldovista].dbo.graduation_term gt
ON gt.graduation_term_id = mba.graduation_term_id
UPDATE u
SET graduation_class_id = gc.graduation_class_id
FROM [user] u
JOIN user_education_mba_school mba
ON mba.user_id = u.user_id
JOIN [sueldovista].dbo.graduation_term gt
ON mba.graduation_term_id = gt.graduation_term_id
JOIN [sueldovista].dbo.graduation_class gc
ON gt.graduation_year_id = gc.graduation_year_id
WHERE gc.site_instance_id = 1
甲骨文:
UPDATE PRODUCTION a
SET (name, count) = (
SELECT name, count
FROM STAGING b
WHERE a.ID = b.ID);
如果您的子查询有多行,请使用 IN
UPDATE db1
SET
field = 1
WHERE id IN (
SELECT id
FROM db2
LEFT JOIN db3
ON db2.id = db3.id
WHERE db3.id IS NOT NULL
)