SQL Join,不确定复杂查询中的确切语法

时间:2011-10-06 16:40:21

标签: mysql sql sql-server

我正在尝试匹配两位数据,添加到现有查询中。

我希望查询本身能解释我正在尝试做什么。我遇到麻烦的是LEFT JOIN从业者p ON insolvencies.practitioner = practitioners.id,

SELECT DISTINCT     i.id            AS  id,
                    i.company       AS  company,
                    i.insolvencyDate AS insolvency_date,
                    i.city          AS  city,
                    i.createdOn     AS  createdOn,
                    1               AS  rank_id,
                    t.entryCopy     AS  insolvency_type,
                    i.businessNature AS business_nature,
                    p.name          AS pract_comp_name,
                    'I'             AS  i_type 
    FROM     insolvencies i, 
        LEFT JOIN practitioners p ON insolvencies.practitioner = practitioners.id, 
        lookupcopy t 
    WHERE    i.checked   =  1    
        AND t.id     =  i.insolvencyType 
        AND i.insolvencyDate    >=  {ts '2010-01-22 00:00:00'} 
        AND i.insolvencyDate    <=  {ts '2011-10-20 00:00:00'} 
        AND (LOWER(p.name)   LIKE   '%Hodgsons%') 
    GROUP BY     id

4 个答案:

答案 0 :(得分:4)

这里有很多问题。首先,我拿出逗号并正确加入lookupcopy。我不是100%肯定你的最终目标,而是从以下几点开始:

SELECT DISTINCT i.id AS id,  
                i.company AS company,  
                i.insolvencyDate AS insolvency_date,  
                i.city AS   city,  
                i.createdOn AS createdOn,  
                1 AS rank_id,  
                t.entryCopy AS insolvency_type,  
                i.businessNature AS business_nature,  
                p.name AS pract_comp_name,  
                'I' AS i_type  
    FROM insolvencies i
        INNER JOIN lookupcopy t  on (t.id = i.insolvencyType)
        LEFT JOIN practitioners p ON i.practitioner = p.id
    WHERE i.checked = 1   
        AND i.insolvencyDate >= {ts '2010-01-22 00:00:00'}  
        AND i.insolvencyDate <= {ts '2011-10-20 00:00:00'}  
        AND (LOWER(p.name)   LIKE '%Hodgsons%')  
    GROUP BY id 

如果您仍然遇到问题,请发布您的实际错误。

另外,另一件事:在where子句中引用p.Name将导致从业者和破产之间的内部联接。那是为了吗?

答案 1 :(得分:2)

您需要删除insolvencies i,之后的逗号才能使LEFT JOIN适用于该表,否则LEFT JOIN会被视为一个独立的表格来交叉申请,这不是无效:

 FROM insolvencies i
          --        ^ remove comma
     LEFT JOIN...

答案 2 :(得分:2)

我认为你正在寻找这样的东西:

SELECT DISTINCT i.id AS id
     , i.company AS company
     , i.insolvencyDate AS insolvency_date
     , i.city AS city
     , i.createdOn AS createdOn
     , 1 AS rank_id
     , t.entryCopy AS insolvency_type
     , i.businessNature AS business_nature
     , p.name AS pract_comp_name
     , 'I'     AS i_type 
  FROM insolvencies i
  LEFT JOIN practitioners p ON insolvencies.practitioner = practitioners.id
  CROSS JOIN lookupcopy t 
  WHERE i.checked   =  1    
    AND t.id     =  i.insolvencyType 
    AND i.insolvencyDate    >=  {ts '2010-01-22 00:00:00'} 
    AND i.insolvencyDate    <=  {ts '2011-10-20 00:00:00'} 
    AND (LOWER(p.name)   LIKE   '%Hodgsons%') 
GROUP BY id

表之间的,CROSS JOIN相同。

但无论如何,您应该阅读cross joininner joinouter join,并在您的情况下使用最合适的内容。

答案 3 :(得分:0)

问题是您的WHERE子句引用了LEFT JOIN'ed表

WHERE    i.checked   =  1    
        AND t.id     =  i.insolvencyType 
        AND i.insolvencyDate    >=  {ts '2010-01-22 00:00:00'} 
        AND i.insolvencyDate    <=  {ts '2011-10-20 00:00:00'} 
        AND (LOWER(p.name)   LIKE   '%Hodgsons%') 

如果LEFT JOIN找不到匹配项,则p.Name将为NULL。通过检查WHERE子句中的p.Name,您将使LEFT JOIN充当FULL JOIN。如果需要检查名称,它应该是LEFT JOIN

中ON表达式的一部分