UPDATE emr_cwagreich.patient_medication a
INNER JOIN
(
SELECT emr_cwagreich.patient_medication_archive.id AS id,
emr_cwagreich.patient_medication_archive.drug_syn_id AS drug_syn_id,
pk.NDC10 AS ndc10,
pk.NDC11 AS ndc11,
pk.PackageID AS package_id,
pd.ProductID AS product_id,
pd.MarketedProductID AS marketed_product_id,
pgns.GenericNameLong AS medication_name
FROM emr_cwagreich.patient_medication_archive
JOIN ep_api.CORE_GENDRUG_SYNONYM cgs ON emr_cwagreich.patient_medication_archive.drug_syn_id = cgs.DRUG_SYN_ID
JOIN ep_api.NDC_PKG_PRODUCT npp ON cgs.DRUG_SYN_ID = npp.DRUG_SYN_ID
JOIN ep_alchemy.package pk ON npp.CORE_10 = pk.NDC10
JOIN ep_alchemy.product pd ON pk.ProductID = pd.ProductID
JOIN ep_alchemy.product_generic_name_stub pgns ON pd.ProductID = pgns.ProductID
GROUP BY medication_name
ORDER BY COUNT(pgns.GenericNameLong) DESC
LIMIT 1
) b ON a.id = b.id
SET a.ndc10 = b.ndc10,
a.ndc11 = b.ndc11,
a.package_id = b.package_id,
a.product_id = b.product_id,
a.marketed_product_id = b.marketed_product_id,
a.medication_name = b.medication_name
WHERE (b.drug_syn_id <> "" AND b.drug_syn_id IS NOT NULL)
AND
(a.product_id = "" OR a.product_id IS NULL)
AND
a.id = 17221
上面的查询有语法错误。我的数据库管理工具(HeidiSQL)告诉我第2行附近有语法错误,但我不能为我的生活弄清楚它在哪里......
任何获得此项目的人都会获得巨大的道具。
致Fabio:错误是(逐字逐句)
SQL Error (1064): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'UPDATE emr_cwagreich.patient_medication a INNER JOIN (SELECT emr_cwagrei' at line 2
我把它缩小到内连接语句中的子选择(就此而言,我通过删除连接减少了这个子选择的复杂性)。 这是:
SELECT pma.id AS id,
pma.drug_syn_id AS drug_syn_id,
pk.NDC10 AS ndc10,
pk.NDC11 AS ndc11,
pk.PackageID AS package_id,
pk.ProductID AS product_id,
pgns.GenericNameLong AS medication_name
FROM emr_cwagreich.patient_medication_archive pma
JOIN ep_api.CORE_GENDRUG_SYNONYM cgs ON pma.drug_syn_id = cgs.DRUG_SYN_ID
JOIN ep_api.NDC_PKG_PRODUCT npp ON cgs.DRUG_SYN_ID = npp.DRUG_SYN_ID
JOIN ep_alchemy.package pk ON npp.CORE_10 = pk.NDC10
JOIN ep_alchemy.product_generic_name_stub pgns ON pk.ProductID = pgns.ProductID
GROUP BY medication_name
ORDER BY COUNT(pgns.GenericNameLong) DESC
LIMIT 1
此查询未返回语法错误。它只需要很长时间(即使我已经在外键上放置了索引......)
答案 0 :(得分:0)
看起来您的SQL语句布局不正确。
您不能在emr_cwagreich.patient_medication
语句之后将表a
别名为UPDATE
,而是在SET
语句之后需要稍后。尝试重新排列您的查询,如下所示:
UPDATE a
SET a.ndc10 = b.ndc10,
a.ndc11 = b.ndc11,
a.package_id = b.package_id,
a.product_id = b.product_id,
a.marketed_product_id = b.marketed_product_id,
a.medication_name = b.medication_name
FROM emr_cwagreich.patient_medication a
INNER JOIN
...
请注意在FROM
之后添加SET
语句,然后您可以为更新表添加别名并加入其他表。
答案 1 :(得分:0)
要正确调试:
在Update中只抛出SELECT,并检查SELECT是否返回了正确的结果,可能是你的错误。我在查询开头没有看到错误,UPDATE emr_cwagreich.patient_medication a INNER JOIN ( ...
似乎是正确的。
这是SELECT中的字段吗?我从来没有见过emr_cwagreich.patient_medication_archive.id
为什么你有三个规格项?
使用新选择尝试更新,并检查是否有效:
UPDATE emr_cwagreich.patient_medication a
INNER JOIN
(
SELECT pma.id AS id,
pma.drug_syn_id AS drug_syn_id,
pk.NDC10 AS ndc10,
pk.NDC11 AS ndc11,
pk.PackageID AS package_id,
pk.ProductID AS product_id,
pgns.GenericNameLong AS medication_name
FROM emr_cwagreich.patient_medication_archive pma
JOIN ep_api.CORE_GENDRUG_SYNONYM cgs ON pma.drug_syn_id = cgs.DRUG_SYN_ID
JOIN ep_api.NDC_PKG_PRODUCT npp ON cgs.DRUG_SYN_ID = npp.DRUG_SYN_ID
JOIN ep_alchemy.package pk ON npp.CORE_10 = pk.NDC10
JOIN ep_alchemy.product_generic_name_stub pgns ON pk.ProductID = pgns.ProductID
GROUP BY medication_name
ORDER BY COUNT(pgns.GenericNameLong) DESC
LIMIT 1
) b ON a.id = b.id
SET a.ndc10 = b.ndc10,
a.ndc11 = b.ndc11,
a.package_id = b.package_id,
a.product_id = b.product_id,
a.marketed_product_id = b.marketed_product_id,
a.medication_name = b.medication_name
WHERE (b.drug_syn_id <> "" AND b.drug_syn_id IS NOT NULL)
AND
(a.product_id = "" OR a.product_id IS NULL)
AND
a.id = 17221