如何使用联接将IN,NOT IN,UNION转换为SQL查询

时间:2019-03-14 14:10:36

标签: sql sql-server tsql

是否可以将具有NOT IN,IN和UNION运算符的子查询转换为联接?我需要使用无连接子查询的查询,因此spring jpa可以理解

 SELECT * 
  FROM CONTACT 
  WHERE partner_idpartner = (
     SELECT partner_idpartner 
     FROM BUSINESSCHANNEL WHERE idBusinessChannel in (
       select idbusinessFrom from business WHERE idbusiness = 5943
     )
   )  
   and idcontact not in (
     SELECT CONTACT_IDCONTACT FROM businesscontact 
     WHERE BUSINESS_IDBUSINESS=5943
   )   
  UNION
  SELECT *  
  FROM CONTACT 
  WHERE partner_idpartner = (
    SELECT partner_idpartner 
    FROM BUSINESSCHANNEL WHERE idBusinessChannel in (
      select idbusinessTo FROM business WHERE idbusiness = 5943
    )
  )  
   and idcontact not in (
    SELECT CONTACT_IDCONTACT FROM businesscontact 
    WHERE BUSINESS_IDBUSINESS=5943
  ) 

4 个答案:

答案 0 :(得分:2)

尝试一下:

SELECT    DISTINCT CONTACT.*
FROM      CONTACT
JOIN      BUSINESSCHANNEL
ON        CONTACT.partner_idpartner = BUSINESSCHANNEL.partner_idpartner
JOIN      business
ON        (BUSINESSCHANNEL.idBusinessChannel = business.idbusinessFrom
AND       business.idbusiness = 5943)
OR        (BUSINESSCHANNEL.idBusinessChannel = business.idbusinessTo)
AND       business.idbusinessroute = 5943)
LEFT JOIN businesscontact
ON        CONTACT.idcontact = businesscontact.CONTACT_IDCONTACT 
AND       BUSINESS_IDBUSINESS = 5943
WHERE     businesscontact.CONTACT_IDCONTACT IS NULL

答案 1 :(得分:2)

您可以将所有这些子查询转换为联接。缺点是您失去了可读性,并且可能会产生巨大的中间结果,您必须使用DISTINCT才能摆脱它。这可能是非常昂贵的。 NOT IN可以转换为反联接(外部联接,然后选择不匹配的行),这是另一种模式,可能会产生不希望的大中间结果。

SELECT DISTINCT c.*  
FROM contact c
JOIN businesschannel bc ON bc.partner_idpartner = c.partner_idpartner
JOIN business b ON bc.idbusinesschannel IN (b.idbusinessfrom, b.idbusinessto)
                AND b.idbusiness = 5943 
LEFT JOIN businesscontact bco ON bco.contact_idcontact = c.idcontact
                              AND bco.business_idbusiness = 5943
WHERE bco.contact_idcontact IS NULL;

答案 2 :(得分:0)

尝试以下查询:我没有测试

DECLARE @tblNotInIUdContact TABLE (  CONTACT_IDCONTACT INT)
DECLARE @tblIdBusinessFrom TABLE (   IdBusiness INT)
DECLARE @tblIdBusinessTo TABLE (     IdBusiness INT)
DECLARE @tblIdPartner TABLE (    partner_idpartner INT)

INSERT INTO @tblNotInIUdContact(CONTACT_IDCONTACT)
SELECT CONTACT_IDCONTACT FROM businesscontact WHERE BUSINESS_IDBUSINESS=5943

INSERT INTO @tblIdBusinessFrom(IdBusiness)
select idbusinessFrom from business  WHERE idbusiness=5943

INSERT INTO @tblIdBusinessTo(IdBusiness)
select idbusinessTo FROM business    WHERE idbusinessroute=5943

INSERT INTO @tblIdPartner(partner_idpartner)
SELECT partner_idpartner FROM BUSINESSCHANNEL bc
INNER JOIN @tblIdBusinessFrom bf ON bc.partner_idpartner = bf.IdBusiness

INSERT INTO @tblIdPartner(partner_idpartner)
SELECT partner_idpartner FROM BUSINESSCHANNEL bc
INNER JOIN @tblIdBusinessTo bf ON bc.partner_idpartner = bf.IdBusiness



SELECT * FROM CONTACT c
    INNER JOIN @tblIdPartner p ON c.partner_idpartner = p.partner_idpartner  
EXCEPT
SELECT * FROM CONTACT c
    INNER JOIN @tblNotInIUdContact ic ON c.idcontact = ic.CONTACT_IDCONTACT

答案 3 :(得分:0)

在这里,你去

  1. 替换为:使用内部联接
  2. 替换不在以下位置的内容: 使用左外部联接tab2,其中tab2.col为null
  3. 替换联合, 我创建一个临时表#mycontact,其结构与 联系

    从1 = 0的联系人中选择*进入我的联系人

    插入#mycontact 选择 *   来自联系人TA

    内部加入   (从BUSINESSCHANNEL T0中选择partner_idpartner   内部联接(从业务WHERE idbusiness = 5943中选择idbusinessFrom)T0.idBusinessChannel = T1.idbusinessFrom上的T1)   左外部联接(来自业务联系人的SELECT CONTACT_IDCONTACT,WHERE BUSINESS_IDBUSINESS = 5943)T2   在T0.idcontact = T2.CONTACT_IDCONTACT上   其中T2.CONTACT_IDCONTACT为null)TA.partner_idpartner = TB.partner_idpartner上的TB

    插入#mycontact

    选择*
      从联系TC   内部联接(

    (SELECT partner_idpartner FROM BUSINESSCHANNEL  T0
    INNER JOIN (  select idbusinessTo FROM business     WHERE idbusinessroute=5943) T1 on T0.idBusinessChannel=T1.idbusinessFrom)  
    
    left outer join ( SELECT CONTACT_IDCONTACT FROM businesscontact WHERE BUSINESS_IDBUSINESS=5943)  T2
    on T0.idcontact=T2.CONTACT_IDCONTACT
    

    其中T2.CONTACT_IDCONTACT为空)

    TD on TC.partner_idpartner=TD.partner_idpartner
    

    从#mycontact

  4. 中选择*