【问题标题】:sql query join for more than 2 tables [closed]超过2个表的sql查询连接[关闭]
【发布时间】:2013-06-06 18:42:01
【问题描述】:

我有 4 张桌子:

 #table 1 columns - areacode,section_number,maint_charge
 #table 2 columns - section_number,discount,fee,total_Maint_charge
 #table 3 columns - areacode, statename
 #table 4 columns - section_number , customer

映射如下:

 #table1.areacode = #table3.areacode
 #table1.section_number = #table2.section_number

我想得到这样的东西:

 state,section_number,maint_charge,maint_charge/total_maint_charge,
 fee*(maint_charge/total_maint_charge) 

谢谢

【问题讨论】:

  • 3 个表连接就像 2 个表连接一样工作。您只需添加另一个 join 子句。
  • 这个问题没有显示任何研究工作。 做好功课很重要。告诉我们您发现了什么以及为什么它不能满足您的需求。这表明您已经花时间尝试帮助自己,它使我们免于重复明显的答案,最重要的是它可以帮助您获得更具体和相关的答案。 FAQ.

标签: sql-server inner-join


【解决方案1】:
SELECT state,
    table1.section_number,
    maint_charge,
    (maint_charge / total_maint_charge),
    (fee * (maint_charge / total_maint_charge))
FROM table1
    JOIN table3 ON table1.areacode=table3.areacode
    JOIN table2 on table1.section_number=table2.section_number;

【讨论】:

    【解决方案2】:

    试试这个 -

    SELECT 
          t3.statename
        , t1.section_number
        , t1.maint_charge
        , t1.maint_charge / t2.total_maint_charge
        , t2.fee * (t1.maint_charge / t2.total_maint_charge)
    FROM #table1 t1
    JOIN #table2 t2 ON t1.section_number = t2.section_number
    JOIN #table3 t3 ON t1.areacode = t3.areacode
    

    【讨论】:

      猜你喜欢
      • 2020-07-16
      • 2019-05-31
      • 2013-10-16
      • 2014-07-18
      • 2021-01-28
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多