 查询购买了客户编号为“000001”的客户购买的所有产品的客户的名称。有三个表:“产品名称”在CP表里,‘客户编号’在CPXSB表里,CP和CPXSB之间通过‘产品编号’连接,‘客户名称’在XSS表里,XSS和CPXSB之间通过‘客户编号’连接。
要怎么写sql语句,好像是用exists 来写,具体怎么写,菜鸟求助一下。

解决方案 »

  1.   

    需求是不是“查找客户编号为“000001”的客户购买的所有产品的名称。”select a.产品名称 from cp a inner join CPXSB b on a.产品编号=b.产品编号 where b.客户编号=‘000001’
      

  2.   


    select distinct t2.客户名称 from CPXSB t1 inner join XSS t2 on t1.客户编号=t2.客户编号
     where 产品编号 in(
    select 产品编号 from CPXSB  where 客户编号='000001')
      

  3.   


    select distinct t2.客户名称 from CPXSB t1 inner join XSS t2 on t1.客户编号=t2.客户编号
     where t1.产品编号 in(
    select 产品编号 from CPXSB  where 客户编号='000001')
      

  4.   

    好吧,我自己写出来了,这里太没爱了
    select distinct XSS.客户名称
    from XSS 
    where  EXISTS
    (
      select *
      from CPXSB CXB1
      WHERE XSS.客户编号=CXB1.客户编号AND not  EXISTS
       (
          select *
          from CPXSB CXB2
          where  CXB2.客户编号='000001' AND not EXISTS
          (
             SELECT *
             FROM CPXSB CXB3
             where CXB3.客户编号=CXB1.客户编号and CXB3.产品编号=CXB2.产品编号
          )
        )
    and XSS.客户编号not in ('000001')
    )