如何通过select的结果更新一列的每一行?
How to update each row of a column by the results of a select?
我想用通过 select 语句获取的值更新列的值。但是通过执行 select 我得到了整套结果,我希望每个结果都被 czi_first_name_en (一种 foreach 循环)替换
这是到目前为止的内容:
UPDATE citizen_info t
SET t.czi_first_name_en=
(
SELECT per_username
FROM person
INNER JOIN enrollment_office
ON person.per_id=enrollment_office.eof_manager_id
INNER JOIN card_request
ON enrollment_office.eof_id=card_request.crq_enroll_office_id
INNER JOIN citizen
ON card_request.crq_citizen_id=citizen.ctz_id
INNER JOIN citizen_info
ON citizen.ctz_id=citizen_info.czi_id
WHERE person.per_dep_id=card_request.crq_enroll_office_id) AS person_username
我该如何进行?
1) 我认为您只需要一个带有 JOIN 的更新(Oracle 风格)。
UPDATE
(
select
per_username as NEW,
czi_first_name_en as OLD
from person
inner join enrollment_office on person.per_id=enrollment_office.eof_manager_id
inner join card_request on enrollment_office.eof_id=card_request.crq_enroll_office_id
inner join citizen on card_request.crq_citizen_id=citizen.ctz_id
inner join citizen_info on citizen.ctz_id=citizen_info.czi_id
where person.per_dep_id=card_request.crq_enroll_office_id
) t
SET t.OLD = t.NEW
另请参阅:
Update statement with inner join on Oracle
2)我想你也可以按照自己的方式去做。
UPDATE citizen_info t
SET t.czi_first_name_en=
(
SELECT
per_username
FROM person
inner join enrollment_office ON person.per_id=enrollment_office.eof_manager_id
inner join card_request ON enrollment_office.eof_id=card_request.crq_enroll_office_id
inner join citizen ON card_request.crq_citizen_id=citizen.ctz_id
inner join citizen_info x ON citizen.ctz_id=x.czi_id
WHERE person.per_dep_id=card_request.crq_enroll_office_id and x.czi_id=t.czi_id
) as person_username
请注意这里唯一的区别:x.czi_id=t.czi_id
。
这会将 x record
链接到您要更新的 t record
。
你必须 "connect" 更新行与 sub-select 行,可能使用一些 id,请参阅我在 sub-select 末尾的尝试:
update Citizen_Info t
set t.czi_first_name_en =
(select per_username
from person inner join enrollment_office
on person.per_id = enrollment_office.eof_manager_id
inner join card_request on
enrollment_office.eof_id = card_request.crq_enroll_office_id
inner join citizen on card_request.crq_citizen_id = citizen.ctz_id
inner join citizen_info on citizen.ctz_id = citizen_info.czi_id
where person.per_dep_id = card_request.crq_enroll_office_id
**and t.perid = person.per_id**)
了解 MERGE
函数 http://en.wikipedia.org/wiki/Merge_%28SQL%29
您指定目标和源,然后在满足匹配条件时执行特定操作,例如更新
您也可以使用仅包含 UPDATE 子句的 MERGE 语句 -
MERGE INTO citizen_info t
USING (SELECT person.per_username per_username,
citizen.ctz_id ctz_id
FROM person
inner join enrollment_office
ON person.per_id = enrollment_office.eof_manager_id
inner join card_request
ON enrollment_office.eof_id =
card_request.crq_enroll_office_id
inner join citizen
ON card_request.crq_citizen_id = citizen.ctz_id
WHERE person.per_dep_id = card_request.crq_enroll_office_id) s
ON(s.ctz_id = t.czi_id)
WHEN matched THEN
UPDATE SET t.czi_first_name_en = s.per_username
/
我想用通过 select 语句获取的值更新列的值。但是通过执行 select 我得到了整套结果,我希望每个结果都被 czi_first_name_en (一种 foreach 循环)替换 这是到目前为止的内容:
UPDATE citizen_info t
SET t.czi_first_name_en=
(
SELECT per_username
FROM person
INNER JOIN enrollment_office
ON person.per_id=enrollment_office.eof_manager_id
INNER JOIN card_request
ON enrollment_office.eof_id=card_request.crq_enroll_office_id
INNER JOIN citizen
ON card_request.crq_citizen_id=citizen.ctz_id
INNER JOIN citizen_info
ON citizen.ctz_id=citizen_info.czi_id
WHERE person.per_dep_id=card_request.crq_enroll_office_id) AS person_username
我该如何进行?
1) 我认为您只需要一个带有 JOIN 的更新(Oracle 风格)。
UPDATE
(
select
per_username as NEW,
czi_first_name_en as OLD
from person
inner join enrollment_office on person.per_id=enrollment_office.eof_manager_id
inner join card_request on enrollment_office.eof_id=card_request.crq_enroll_office_id
inner join citizen on card_request.crq_citizen_id=citizen.ctz_id
inner join citizen_info on citizen.ctz_id=citizen_info.czi_id
where person.per_dep_id=card_request.crq_enroll_office_id
) t
SET t.OLD = t.NEW
另请参阅:
Update statement with inner join on Oracle
2)我想你也可以按照自己的方式去做。
UPDATE citizen_info t
SET t.czi_first_name_en=
(
SELECT
per_username
FROM person
inner join enrollment_office ON person.per_id=enrollment_office.eof_manager_id
inner join card_request ON enrollment_office.eof_id=card_request.crq_enroll_office_id
inner join citizen ON card_request.crq_citizen_id=citizen.ctz_id
inner join citizen_info x ON citizen.ctz_id=x.czi_id
WHERE person.per_dep_id=card_request.crq_enroll_office_id and x.czi_id=t.czi_id
) as person_username
请注意这里唯一的区别:x.czi_id=t.czi_id
。
这会将 x record
链接到您要更新的 t record
。
你必须 "connect" 更新行与 sub-select 行,可能使用一些 id,请参阅我在 sub-select 末尾的尝试:
update Citizen_Info t
set t.czi_first_name_en =
(select per_username
from person inner join enrollment_office
on person.per_id = enrollment_office.eof_manager_id
inner join card_request on
enrollment_office.eof_id = card_request.crq_enroll_office_id
inner join citizen on card_request.crq_citizen_id = citizen.ctz_id
inner join citizen_info on citizen.ctz_id = citizen_info.czi_id
where person.per_dep_id = card_request.crq_enroll_office_id
**and t.perid = person.per_id**)
了解 MERGE
函数 http://en.wikipedia.org/wiki/Merge_%28SQL%29
您指定目标和源,然后在满足匹配条件时执行特定操作,例如更新
您也可以使用仅包含 UPDATE 子句的 MERGE 语句 -
MERGE INTO citizen_info t
USING (SELECT person.per_username per_username,
citizen.ctz_id ctz_id
FROM person
inner join enrollment_office
ON person.per_id = enrollment_office.eof_manager_id
inner join card_request
ON enrollment_office.eof_id =
card_request.crq_enroll_office_id
inner join citizen
ON card_request.crq_citizen_id = citizen.ctz_id
WHERE person.per_dep_id = card_request.crq_enroll_office_id) s
ON(s.ctz_id = t.czi_id)
WHEN matched THEN
UPDATE SET t.czi_first_name_en = s.per_username
/