如何使用另一个表执行查询结果的LEFT JOIN?

时间:2016-04-19 11:30:15

标签: mysql

我在同一个数据库中有两个名为DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1DE1_0_2008_BENEFICIARY_SUMMARY_FILE_SAMPLE_1的表。

我必须对DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1执行查询,然后使用`DE1_0_2008_BENEFICIARY_SUMMARY_FILE_SAMPLE_1'执行此查询结果的LEFT JOIN。单独的查询导致1178行,这很好。但我无法进行LEFT JOIN。我使用了这个SQL查询:

SELECT * 
FROM `DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1` 
LEFT JOIN DE1_0_2008_BENEFICIARY_SUMMARY_FILE_SAMPLE_1 
ON DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1.DESYNPUF_ID = DE1_0_2008_BENEFICIARY_SUMMARY_FILE_SAMPLE_1.DESYNPUF_ID
WHERE DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1.ICD9_DGNS_CD_1 = 7243 OR DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1.ICD9_DGNS_CD_2 = 7243 OR DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1.ICD9_DGNS_CD_3 = 7243 OR ICD9_DGNS_CD_4 = 7243

1 个答案:

答案 0 :(得分:1)

此查询技术上很好。

请注意,它可以更优雅地重写如下,但实际上你需要仔细研究分区,最重要的是,规范化......

SELECT * 
  FROM DE1_0_2008_TO_2010_OUTPATIENT_CLAIMS_SAMPLE_1 o 
  LEFT 
  JOIN DE1_0_2008_BENEFICIARY_SUMMARY_FILE_SAMPLE_1 b
    ON o.DESYNPUF_ID = b.DESYNPUF_ID
 WHERE 7243 IN(o.ICD9_DGNS_CD_1,o.ICD9_DGNS_CD_2,o.ICD9_DGNS_CD_3,o.ICD9_DGNS_CD_4);