开发者

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

开发者 https://www.devze.com 2023-03-31 08:13 出处:网络
I have a query I need to perform on a table that is roughly 1M records.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 que

I have a query I need to perform on a table that is roughly 1M records. 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.

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.

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.

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

Secondary table containing records (1 each) with the "Practice" field I want to pull.

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

A Quick glance of the rela开发者_开发知识库tionship i'm talking about

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

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.

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. Any help would be greatly appreciated.

Thank you,

Silver Tiger


You have to join the taxonomy_codes table multiple times:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消