0

我编写了以下脚本:

SELECT vil.account_id , 
       vil.imp_id, 
       vil.owner ,
       vil.start_date,  
       CASE  
         WHEN ac.status = 4  
         THEN status_date  
         ELSE NULL  
       END AS Live_date,
       cs.country_code  
  FROM ACCOUNT ac  
  INNER JOIN vu_imp_list vil  
     ON vil.account_id = ac.account_id  
  INNER JOIN entity e  
     ON ac.entity_id = e.entity_id  
  INNER JOIN country_specifics cs  
     ON e.country_id = cs.country_id  
WHERE ac.account_id IN
('000206789', '000207464', '000207696', '000207746')  
AND
(vil.start_date =  
(SELECT MIN(vils.start_date)
FROM vu_imp_list vils  
WHERE vil.account_id = vils.account_id) 

这将为imp_id每个account_id. 但是,它将忽略那些没有imp_id. 因为有些account_ids人没有imp_id在餐桌上vu_imp_listWHERE's second condition只会提取那些有对应start_date和的记录imp_id。简而言之,如果在 table 中找不到相应的记录,如何忽略此条件vu_imp_list。我发现这篇文章与 MySQL 相关,但似乎有问题,因为我不断收到错误missing SELECT keyword

SELECT vil.account_id , 
       vil.imp_id, 
       vil.owner ,
       vil.start_date,  
       CASE  
         WHEN ac.status = 4  
         THEN status_date  
         ELSE NULL  
       END AS Live_date,
       cs.country_code  
  FROM ACCOUNT ac  
  INNER JOIN vu_imp_list vil  
     ON vil.account_id = ac.account_id  
  INNER JOIN entity e  
     ON ac.entity_id = e.entity_id  
  INNER JOIN country_specifics cs  
     ON e.country_id = cs.country_id  
WHERE ac.account_id IN
('000206789', '000207464', '000207696', '000207746')  
AND
((vil.start_date =  
(SELECT MIN(vils.start_date)
FROM vu_imp_list vils  
WHERE vil.account_id = vils.account_id))  
OR  
NOT EXISTS  
(vil.start_date = (SELECT MIN(vils.start_date)  
FROM obi.vu_implementation_list vils  
WHERE vil.ob10_account_id = vils.ob10_account_id)
4

1 回答 1

1

如果我对您的理解正确,您需要的是LEFT JOIN在加入ACCOUNT表时VU_IMP_LIST更改您的条件 inWHERE子句以检查是否start_dateNULL(未找到相应的记录VU_IMP_LISTOR它是否等于从标量子查询返回的值。使用LEFT JOIN,将返回表中的所有记录ACCOUNT,即使表中没有对应的记录VU_IMP_LIST

SELECT vil.account_id , vil.imp_id, vil.owner , vil.start_date,
       CASE
         WHEN ac.status = 4 THEN status_date
         ELSE NULL
       END AS Live_date, cs.country_code
  FROM ACCOUNT ac
    LEFT JOIN vu_imp_list vil ON vil.account_id = ac.account_id
    INNER JOIN entity e ON ac.entity_id = e.entity_id
    INNER JOIN country_specifics cs ON e.country_id = cs.country_id
WHERE ac.account_id IN ('000206789', '000207464', '000207696', '000207746')
  AND (vil.start_date IS NULL
       OR vil.start_date = (SELECT MIN(vils.start_date)
                              FROM vu_imp_list vils
                            WHERE vil.account_id = vils.account_id))
;

您可以在此处阅读有关不同类型连接的更多信息:关于 SQL 连接

于 2013-10-24T12:02:41.733 回答