【问题标题】:where condition failed on multiple inner joins in sqlwhere 条件在 sql 中的多个内部联接上失败
【发布时间】:2015-11-26 22:16:53
【问题描述】:

我正在尝试内部连接多个列,但在 where 条件下导致列无效。 查询:

select t_contract,t_complete,t_zone_name, t_street_name, t_vrm, t_make, t_model,t_colour,t_date_finally_settled,t_cancelled,t_offence_code,
    t_date_time_issued,    CONVERT(VARCHAR, t_date_time_issued, 103) as date_issued,   CONVERT(VARCHAR, t_date_time_issued, 108) AS time_issued,   te_event, lhr.*  
        FROM tickets t  
        LEFT OUTER JOIN   
         (  
         SELECT DISTINCT thr.thr_system_ref, hr.hr_description AS latest_on_hold_reason  
                FROM dbo.tickets t  
                INNER JOIN dbo.ticket_hold_record thr  
                ON (t.t_number=thr.thr_system_ref)  
                INNER JOIN  
                (  
                    SELECT mthr.thr_system_ref, MAX(mthr.thr_on_hold_date) AS m_thr_on_hold_date  
                    FROM dbo.ticket_hold_record mthr  
                    GROUP BY mthr.thr_system_ref  
                ) latest  
                ON (thr.thr_system_ref=latest.thr_system_ref AND thr.thr_on_hold_date=latest.m_thr_on_hold_date)  
                INNER JOIN dbo.hold_reasons hr    " ON (thr.thr_hold_type=hr.hr_code) where hr_code in (' 2260675','2793360','2810778','2903324','2420135')
                ON (thr.thr_hold_type=hr.hr_code)  
            )lhr  
            ON (t.t_number=lhr.thr_system_ref)  

         Inner JOIN  
           ticket_events  te  
         ON ( t.t_number = te.te_system_ref)  

        where  thr.thr_hold_type = '2420135' 
        and  convert (datetime,te_date,101) between convert(datetime,'2015/11/01',101)
           and convert (datetime,'2015/11/25',101) 

错误: 列前缀“thr”与查询中使用的表名或别名不匹配。

【问题讨论】:

  • 一次只删除一个条件,看看它什么时候执行没有错误。然后你可以纠正它。 (提示:其中 thr.thr_hold_type = '2420135' ... 进入子查询。)
  • 从您的外部选择中看不到 thr 表。您应该将条件放在从此表中选择的查询上
  • Jarlh 只有一个条件,该列来自多个内部连接,我使用了最新的 lhr 别名,但仍然失败。
  • 为什么不能有 thr.thr_hold_type = '2420135' 作为子查询的 WHERE。
  • 我需要在 where 条件下使用该列。

标签: sql


【解决方案1】:

与以前相同的查询,但我已将 thr.thr_hold_type = '2420135' 条件移到子查询中以进入范围(也不会与 distinct 混淆):

select t_contract,t_complete,t_zone_name, t_street_name, t_vrm, t_make, t_model,t_colour,t_date_finally_settled,t_cancelled,t_offence_code,
    t_date_time_issued,    CONVERT(VARCHAR, t_date_time_issued, 103) as date_issued,   CONVERT(VARCHAR, t_date_time_issued, 108) AS time_issued,   te_event, lhr.*  
        FROM tickets t  
        LEFT OUTER JOIN   
         (  
         SELECT DISTINCT thr.thr_system_ref, hr.hr_description AS latest_on_hold_reason  
                FROM dbo.tickets t  
                INNER JOIN dbo.ticket_hold_record thr  
                ON (t.t_number=thr.thr_system_ref)  
                INNER JOIN  
                (  
                    SELECT mthr.thr_system_ref, MAX(mthr.thr_on_hold_date) AS m_thr_on_hold_date  
                    FROM dbo.ticket_hold_record mthr  
                    GROUP BY mthr.thr_system_ref  
                ) latest  
                ON (thr.thr_system_ref=latest.thr_system_ref AND thr.thr_on_hold_date=latest.m_thr_on_hold_date)  
                INNER JOIN dbo.hold_reasons hr  
                ON (thr.thr_hold_type=hr.hr_code)  
                WHERE thr.thr_hold_type = '2420135' 
            )lhr  
            ON (t.t_number=lhr.thr_system_ref)  

         Inner JOIN  
           ticket_events  te  
         ON ( t.t_number = te.te_system_ref)  

        where convert (datetime,te_date,101) between convert(datetime,'2015/11/01',101)
           and convert (datetime,'2015/11/25',101) 

【讨论】:

  • 谢谢我更新了我的代码,内部连接有多个条件,我应该在 where 条件下使用那个特定的列..
  • 很高兴听到你已经掌握了它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 2018-06-19
  • 2017-07-31
  • 2012-05-12
  • 2011-07-10
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多