【发布时间】: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