简体   繁体   English

MySQL连接具有许多(字段)到一个(辅助表)关系

[英]MySQL Join with many (fields) to one (secondary table) relationship

I have a query I need to perform on a table that is roughly 1M records. 我有一个查询,我需要在大约100万条记录的表上执行。 I am trying to reduce the churn, but unfortunately there is a UNION involved (after i figure this join out), so that may be a question for another day. 我正在尝试减少用户流失,但是不幸的是,涉及到一个UNION(在我确定加入后),所以这可能是另一个问题。

The records and data I need to get reference 3 fields in a table that need each pull a description from another table and return it in the same record, but when i do the Inner join i was thinking, it either returns only 1 field fromt he other table, or multiple records from he original table. 我需要获取表中3个字段的引用的记录和数据,每个字段都需要从另一个表中提取描述并在同一条记录中返回它,但是当我执行内部连接时,我想,它要么仅返回1个字段其他表或原始表中的多个记录。

Here are some screen shots of the tables and their relationship: 以下是一些表格及其关系的屏幕截图:

Primary table containing records (1 each) with the physician record I want to pull, including up to 3 codes that can be listed in the "taxonomy" table. 主表包含记录(每个记录1条)和我要提取的医生记录,包括最多3个可以在“分类”表中列出的代码。

主表

Secondary table containing records (1 each) with the "Practice" field I want to pull. 辅助表包含我要提取的带有“ Practice”字段的记录(每条记录1条)。

次要标签

A Quick glance of the relationship i'm talking about 我正在谈论的关系一目了然

在此处输入图片说明

I presume that if perform an inner join matching the 3 fields in the physicians table, that it will have to iterate that table multiple times to pull each taxonomy code .. but I still can't even figure the syntax to easily pull all of these codes instead of just 1 of them. 我认为,如果执行与Doctors表中的3个字段匹配的内部联接,它将不得不对该表进行多次迭代以提取每个分类法代码..但是我什至无法弄清楚轻松提取所有这些语法的语法代码而不是其中的1个。

i've tried this: 我已经试过了:

SELECT
taxonomy_codes.specialization,
physicians.provider_last_name,
physicians.provider_first_name,
physicians.provider_dba_name,
physicians.legal_biz_name,
physicians.biz_practice_city
FROM
taxonomy_codes
INNER JOIN physicians ON physicians.provider_taxonomy_code_1 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_2 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_3 = taxonomy_codes.taxonomy_codes

First, the query churns a lot and it only returns one taxonomy specialty result which I presume is because of the OR in the join statement. 首先,查询非常麻烦,并且它仅返回一个分类专业结果,我认为这是因为join语句中的OR。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank you, 谢谢,

Silver Tiger 银虎

You have to join the taxonomy_codes table multiple times: 您必须多次加入taxonomy_codes表:

SELECT p.provider_last_name, p...., t1.specialization as specialization1, t2.specialization as specialization2, t3.specialization as specialization3
FROM physicians p
LEFT JOIN taxonomy_codes t1 ON t1.taxonomy_codes = provider_taxonomy_code_1
LEFT JOIN taxonomy_codes t2 ON t2.taxonomy_codes = provider_taxonomy_code_2
LEFT JOIN taxonomy_codes t3 ON t3.taxonomy_codes = provider_taxonomy_code_3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM