【问题标题】:Where clause on INNER JOININNER JOIN 的 Where 子句
【发布时间】:2017-10-01 08:29:24
【问题描述】:

我有四张桌子。我需要从他们所有人那里获取数据。表 'Tenancy_histories' 包含 move_in_date、move_out_date、rent 列。 'Profiles' 包含 first_name、last_name、email、profile_id 等。'Referrals' 包含referrer_bonus_amount 和类似的其他数据。

最重要的是,它包含特定 profile_id 的推荐次数,即“referrer_id(same as profile id)”列中该 profile_id 的出现次数。 'Houses' 包含租户占用的房屋详细信息。表 'Houses''Profiles' 没有直接链接,而是通过表 'Tenancy_histories'

链接

我需要编写一个查询来获取一次都没有推荐过的租户的全名、联系方式、城市和房屋详细信息。

我尝试了类似的方法,但没有得到所需的输出,但没有收到任何错误

SELECT
    pr.first_name + ' ' + pr.last_name AS full_name, pr.phone, 
    pr.[city(hometown)], hs.bhk_details 
FROM
    Profiles pr  
INNER JOIN 
    Tenancy_histories th ON pr.profile_id = th.profile_id 
INNER JOIN 
    Houses hs ON th.house_id = hs.house_id 
INNER JOIN 
    Referrals rf ON pr.profile_id = rf.[referrer_id(same as profile id)] 
WHERE 
    pr.profile_id NOT IN (SELECT [referrer_id(same as profile id)] 
                          FROM Referrals)

【问题讨论】:

    标签: sql sql-server tsql


    【解决方案1】:

    试试这个:

    select full_name , phone,[city(hometown)], bhk_details ,profile_id 
    from (
    select p.full_name , p.phone,p.[city(hometown)], p.bhk_details ,p.profile_id
    from (
    select pr.first_name+' '+pr.last_name as full_name, pr.phone, pr.[city(hometown)], hs.bhk_details ,pr.profile_id
    from Profiles pr 
    INNER JOIN 
    Tenancy_histories th 
    on pr.profile_id = th.profile_id 
    INNER JOIN 
    Houses hs 
    on th.house_id = hs.house_id 
    ) as p
    left join
    Referrals rf 
    on p.profile_id = rf.[referrer_id(same as profile id)] 
    where rf.[referrer_id(same as profile id)] is null
    ) as p_r
    

    【讨论】:

      【解决方案2】:

      只需将 INNER JOIN 删除到引荐表即可解决问题

      SELECT
          pr.first_name + ' ' + pr.last_name AS full_name, 
          pr.phone, 
          pr.[city(hometown)], 
          hs.bhk_details 
      FROM
          Profiles pr  
      INNER JOIN 
          Tenancy_histories th ON pr.profile_id = th.profile_id 
      INNER JOIN 
          Houses hs ON th.house_id = hs.house_id 
      WHERE 
          pr.profile_id NOT IN (SELECT [referrer_id(same as profile id)] 
                                FROM Referrals)
      

      【讨论】:

        【解决方案3】:

        NOT IN 带有子查询是危险的。如果子查询中的任何行返回 NULL 值,则不会返回任何行。

        除了将inner join 删除为referrals 之外,我建议将比较更改为使用NOT EXISTS

        WHERE NOT EXISTS (SELECT 1
                          FROM referrals
                          WHERE pr.profile_id = r.[referrer_id(same as profile id)] 
                         );
        

        另一种方法是使用LEFT JOINreferrals 并简单地检查没有匹配:

        SELECT pr.first_name + ' ' + pr.last_name AS full_name, pr.phone, 
               pr.[city(hometown)], hs.bhk_details 
        FROM Profiles pr INNER JOIN 
             Tenancy_histories th
             ON pr.profile_id = th.profile_id INNER JOIN 
             Houses hs
             ON th.house_id = hs.house_id LEFT JOIN 
             Referrals rf
             ON pr.profile_id = rf.[referrer_id(same as profile id)] 
        WHERE rf.[referrer_id(same as profile id)] IS NULL;
        

        【讨论】:

        • 谢谢。我一定会注意 NOT IN 子句
        猜你喜欢
        • 2013-08-11
        • 2010-11-04
        • 2012-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-07
        • 2015-09-17
        • 2019-01-15
        相关资源
        最近更新 更多