简体   繁体   English

仅两个表之间的多个联接

[英]Multiple joins between only two tables

Table1: key_item#, code_1, code_2, code_3 表1:key_item#,code_1,code_2,code_3

Table2: uniq_code, code_desc 表2:uniq_code,code_desc

Table1: 表格1:

key_item#  code_1  code_2  code_3
   1         Y01    M02     X01
   2         Y01    M04     X01

Table2: 表2:

uniq_code      code_desc
   Y01       DescriptionY01
   M02       DescriptionM02
   X01       DescriptionX01
   M04       DescriptionM04

Better image detail https://dl.dropboxusercontent.com/u/9951225/Untitled-4.jpg 更好的图像细节https://dl.dropboxusercontent.com/u/9951225/Untitled-4.jpg

Query Result 查询结果

key_item#   code_1     code_desc      code_2        code_desc    cod_3    code_desc
   1          Y01    DescriptionY01     M02     DescriptionM02    X01   DescriptionX01
   2          Y01    DescriptionY01     M04     DescriptionM04    X01   DescriptionX01

Query 询问

SELECT     Table1.key_item#,
           Table1.code_1,
           Table2.code_desc,
           Table1.code_2,
           Table2.code_desc, 
           imc_iamerican_claim_lines.Add_diagnosis1 

FROM       msp_dx9_priority.dx09code_matrix 
INNER JOIN Table1 ON Table1.code_1 = Table2.uniq_code 
INNER JOIN Table1 ON Table1.code_2 = Table2.uniq_code

I can't go over the "Not unique table Alias" error 我无法查看“非唯一表别名”错误

You need to alias your joins, so that the query engine knows how to disambiguate the first Table1 from the second Table1. 您需要为联接加上别名,以便查询引擎知道如何从第二个Table1消除第一个Table1的歧义。 Documentation here: http://dev.mysql.com/doc/refman/5.0/en/join.html 此处的文档: http : //dev.mysql.com/doc/refman/5.0/en/join.html

Here is an example: 这是一个例子:

SELECT     Table1.key_item#, -- which join should this come from? code1 or code2?
           t1_code1.code_1,
           Table2.code_desc, -- don't know what is Table2, perhaps t1_code1?
           t1_code2.code_2,
           Table2.code_desc, -- don't know what is Table2, perhaps t1_code2?
           imc_iamerican_claim_lines.Add_diagnosis1  -- also don't know what this is :)

FROM       msp_dx9_priority.dx09code_matrix 
INNER JOIN Table1 t1_code1 ON t1_code1.code_1 = Table2.uniq_code 
INNER JOIN Table1 t1_code2 ON t1_code2.code_2 = Table2.uniq_code

I found this approach 我发现这种方法

 SELECT t1.key, t1.code_1, t21.`desc` as desc_1, t1.code_2, t22.`desc` as desc2, t1.code_3, t23.`desc` as desc3 FROM t1 LEFT JOIN t2 t21 on t1.code_1 = t21.code LEFT JOIN t2 t22 ON t1.code_2 = t22.code LEFT JOIN t2 t23 ON t1.code_3 = t23.code; 

With this reference I did my final report. 以此参考,我完成了最终报告。 Just in case someone is going to need it. 以防万一有人需要它。

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

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