MySQL 使用多个 ON 条件连接到同一个表?
我仍然是一个 mysql 新手,我广泛查看了之前的问题,试图找到合适的解决方案。
我有两个表 CaseReportsTempImport2011Q3
和 FDADrugsDB
,我想进行左连接以将 CaseReportsTempImport2011Q3.drugname
与 FDADrugsDB.ReferenceDrugName
匹配code> 和 FDADrugsDB.DrugName
我的设想是这样的:
SELECT DISTINCT(CaseReportsTempImport2011Q3.DRUGNAME)
, FDADrugsDB_Product.drugname
, FDADrugsDB_Product.ReferenceDrug
, FDADrugsDB_Product.activeingred
FROM CaseReportsTempImport2011Q3
LEFT JOIN FDADrugsDB_Product ON CaseReportsTempImport2011Q3.DRUGNAME LIKE TRIM(FDADrugsDB_Product.ReferenceDrug)
LEFT JOIN FDADrugsDB_Product ON CaseReportsTempImport2011Q3.DRUGNAME LIKE TRIM(FDADrugsDB_Product.DrugName)
ORDER BY LENGTH(CaseReportsTempImport2011Q3.DRUGNAME) ASC
但是这不起作用我得到“不是唯一的表/别名:'FDADrugsDB_Product'” - 有帮助吗?
感谢
编辑更好的解决方案请求/重新表述 Per borealids“但是,我不确定这是否是您想要做的 - 连接表两次将产生乘数的结果。我认为您可能想要一个带有 ON ... OR ... 的连接,使得连接条件是两个链接原因的“或”。”
我想知道该怎么做。
解决方案 ON CaseReportsTempImport2011Q3.DRUGNAME LIKE TRIM(FDADrugsDB_Product.ReferenceDrug) 或 CaseReportsTempImport2011Q3.DRUGNAME LIKE TRIM(FDADrugsDB_Product.DrugName
感谢 Tom 和 borealid。
Still a mysql newb and I looked extensively through previous questions trying to find an appropriate solution.
I have two tables CaseReportsTempImport2011Q3
and FDADrugsDB
, I want to do a left join to match CaseReportsTempImport2011Q3.drugname
to FDADrugsDB.ReferenceDrugName
and FDADrugsDB.DrugName
What I envisioned was something like:
SELECT DISTINCT(CaseReportsTempImport2011Q3.DRUGNAME)
, FDADrugsDB_Product.drugname
, FDADrugsDB_Product.ReferenceDrug
, FDADrugsDB_Product.activeingred
FROM CaseReportsTempImport2011Q3
LEFT JOIN FDADrugsDB_Product ON CaseReportsTempImport2011Q3.DRUGNAME LIKE TRIM(FDADrugsDB_Product.ReferenceDrug)
LEFT JOIN FDADrugsDB_Product ON CaseReportsTempImport2011Q3.DRUGNAME LIKE TRIM(FDADrugsDB_Product.DrugName)
ORDER BY LENGTH(CaseReportsTempImport2011Q3.DRUGNAME) ASC
But that doesn't work I get 'Not unique table/alias: 'FDADrugsDB_Product'' - Any help?
Thanks
EDIT FOR BETTER SOLUTION REQUEST/REPHRASE
Per borealids "However, I'm not sure this is what you want to do - joining the table twice will produce a multiplicative number of results. I think you might have wanted one join with an ON ... OR ..., making the join condition an "or" of the two reasons for linkage."
I would like to know how to do that.
SOLUTION
ON CaseReportsTempImport2011Q3.DRUGNAME LIKE TRIM(FDADrugsDB_Product.ReferenceDrug) OR CaseReportsTempImport2011Q3.DRUGNAME LIKE TRIM(FDADrugsDB_Product.DrugName
Thanks tom and borealid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了使您编写的查询有效,您需要为同一个表的两次使用分配两个不同的关系名称。
然后
使用名称
FDA_first
和FDA_second
来明确引用每组结果。否则,当您说FDADrugsDB_Product
时,查询引擎无法判断您的意思 - 其中有两个,每个都不同!但是,我不确定这是否是您想要做的 - 连接表两次将产生乘数的结果。我认为您可能想要一个带有
ON ... OR ...
的连接,使连接条件成为两个链接原因的“或”。To make the query you wrote valid, you need to assign two different relation names to the two uses of the same table.
and
and then use the names
FDA_first
andFDA_second
to refer to results from each set distinctly. Otherwise the query engine can't tell what you mean when you sayFDADrugsDB_Product
- there are two of them, each different!However, I'm not sure this is what you want to do - joining the table twice will produce a multiplicative number of results. I think you might have wanted one join with an
ON ... OR ...
, making the join condition an "or" of the two reasons for linkage.您需要为表提供别名,否则 MySql 将不知道您正在谈论的是 FDADrugsDB 表的哪个实例。 IE
You need to give the tables alias names, otherwise MySql won't know which instance of the FDADrugsDB table you are talking about. i.e.
请注意,您可能应该使用
=
而不是LIKE
,除非ReferenceDrug
和DrugName
包含匹配模式。Note that you should probably use
=
instead ofLIKE
, unlessReferenceDrug
andDrugName
contain match patterns.