要解决此问题,我们需要更新几千行,其中有一个尚未通过验证的管理实体,但在已验证的列表中有一个匹配的管理实体名称。
我编写了脚本来从中将2个表连接在一起的行。这给出了需要更新的行列表。
SELECT
pi.id AS `Primary Key for PI`,
pi.admin_entity_id,
pi.admin_entity_unverified,
asa.id AS `Admin Setup Accounts ID`,
asa.name AS `Admin Entity-Verified`
FROM
p_information pi
LEFT JOIN admin_setup_accounts asa ON asa.name =
pi.admin_entity_unverified
WHERE
pi.admin_entity_unverified IS NOT NULL
AND pi.admin_entity_unverified <> ''
AND pi.admin_entity_id IS NULL
AND pi.admin_entity_unverified IN (select name FROM
admin_setup_accounts)
http://sqlfiddle.com/#!9/a8c11a/3/0
此查询返回未验证条目的列表(pi.admin_entity_unverified),它们与已验证列表(asa.name)和每个已验证名称的ID(asa.id)相匹配。
我现在需要对所有这些匹配的行进行更新,并使用相应的管理员设置帐户ID(asa.id)更新pi表admin_entity_id,因此我想这样做
update p_information pi
inner join (
SELECT
pi.id AS `Primary Key for PI`,
pi.admin_entity_id,
pi.admin_entity_unverified,
asa.id AS `Admin_Setup_ Accounts_ID`,
asa.name AS `Admin Auditing Entity-Verified`
FROM
p_information pi
LEFT JOIN admin_setup_accounts asa ON asa.name =
pi.admin_auditing_entity_unverified
WHERE
pi.admin_auditing_entity_unverified IS NOT NULL
AND pi.admin_auditing_entity_unverified <> ''
AND pi.admin_auditing_entity_id IS NULL
AND pi.admin_auditing_entity_unverified IN (select name FROM
admin_setup_accounts)) upi
ON upi.id = pi.id
SET pi.admin_entity_id = (this needs to be the asa.id AS `Admin Setup Accounts ID` returned from the subquery (upi)
但是由于pi和asa都使用子查询中的id列,我如何在查询之外列出那些?还是有另一种方法可以使用查询完成此更新?让我知道
谢谢
答案 0 :(得分:0)
好吧,我想我已经弄清楚了,我想我不能在子查询之外使用列别名,但是它看起来很好用。我按照set pi.admin_entity_id = upi.Admin_Setup_ Accounts_ID的方式执行set命令,它可以正确更新。 :)哦,好吧,不应该多想一下