【问题标题】:Basic SQL join question. Can you help me improve my skillset?基本 SQL 连接问题。你能帮我提高我的技能吗?
【发布时间】:2009-11-13 02:53:27
【问题描述】:

好的.. 所以我正在努力提高我的 SQL 技能并有一个问题。这是架构的屏幕截图。

Schema http://img509.imageshack.us/img509/97/screenhunter02nov121946.gif
(http://img509.imageshack.us/img509/97/screenhunter02nov121946.gif)

好的,我正在从您可以看到的表格中选择一堆报表包和其他行。我已经将这两个表正确连接在一起并显示应该返回的内容。现在我需要在我的结果行中添加另一个字段,说明这是什么类型的报告。如何通过 ReportBundleGroup 表加入 ReportGroupType 表而不获得结果?

这是我目前使用的查询。

SELECT * FROM ReportBundleCustomerVisibility INNER JOIN ReportBundle ON ReportBundleCustomerVisibility.ReportBundleID = ReportBundle.ID WHERE ReportBundleCustomerVisibility.ReferenceCustomerID = 2303

再次感谢所以

【问题讨论】:

标签: sql sql-server join


【解决方案1】:
SELECT * 
  FROM ReportBundleCustomerVisibility AS v
    JOIN ReportBundle AS b ON b.ID = v.ReportBundleID
    JOIN ReportBundleGroup AS g ON b.ID = g.ReportBundleID
    JOIN ReportGroupTYpe AS t ON t.ID = g.ReportGroupTypeID
WHERE v.ReferenceCustomerID = 2303

【讨论】:

    【解决方案2】:

    听起来你只需要另一个内部连接来获取你需要的信息。您可以将第二个连接视为将连接结果与 ReportGroupType 表连接起来。我添加了括号以尝试加入第二个 INNER JOIN 正在运行的两个集合。

    SELECT * FROM ((ReportBundleCustomerVisibility 
        INNER JOIN ReportBundle ON ReportBundleCustomerVisibility.ReportBundleID = ReportBundle.ID) 
        INNER JOIN ReportGroupType ON ReportBundleGroup.ReportGroupTypeID = ReportGroupType.ID)
        WHERE ReportBundleCustomerVisibility.ReferenceCustomerID = 2303
    

    我还强烈建议不要在生产代码或您计划重用的任何查询中使用“SELECT *”,因为表架构可能会更改并可能影响报告和 UI。改为显式指定列。

    【讨论】:

      猜你喜欢
      • 2021-02-04
      • 2023-02-01
      • 2014-03-01
      • 2019-09-13
      • 2016-05-21
      • 2020-09-03
      • 2011-01-24
      • 2021-08-14
      • 1970-01-01
      相关资源
      最近更新 更多