[英]Where clause on INNER JOIN
I have four tables. 我有四张桌子。 I need to get data from all of them. 我需要从所有这些数据中获取数据。 Table 'Tenancy_histories' contains the move_in_date, move_out_date, rent columns. 表'Tenancy_histories'包含move_in_date,move_out_date,rent列。 'Profiles' contains the first_name, last_name, email, profile_id etc. 'Referrals' contains the the referrer_bonus_amount and similar other data. “配置文件”包含first_name,last_name,email,profile_id等。 “引用”包含referrer_bonus_amount和类似的其他数据。
Most importantly it contains the number of referrals made by a particular profile_id which is the number of occurrence of that profile_id in the 'referrer_id(same as profile id)' column. 最重要的是,它包含特定profile_id所引用的引用数,该引用数是'referrer_id(与profile id相同)列中该profile_id的出现次数。 'Houses' contains the house details occupied by the tenants. '房屋'包含租户占用的房屋细节。 Table 'Houses' and 'Profiles' are not directly linked but linked through table 'Tenancy_histories' 表'房屋'和'个人资料'没有直接关联,而是通过表'Tenancy_histories'链接
I need to write a query to get Fullname, Contact, City and House Details of the tenants who have not referred even once. 我需要写一个查询来获取尚未提及一次的租户的姓名,联系方式,城市和房屋详细信息。
I tried something like this but is not getting the desired ouput though not getting any error 我试过这样的事情,但没有得到所需的输出,虽然没有得到任何错误
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)
Try this: 尝试这个:
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
Simply removing the INNER JOIN to referrals table should do the trick 简单地删除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)
NOT IN
with a subquery is dangerous. NOT IN
子查询是危险的。 If any row in the subquery returns a NULL
value then no rows are ever returned. 如果子查询中的任何行返回NULL
值,则不会返回任何行。
In addition to removing the inner join
to referrals
, I would recommend changing the comparison to using NOT EXISTS
: 除了将inner join
移除到referrals
,我建议将比较更改为使用NOT EXISTS
:
WHERE NOT EXISTS (SELECT 1
FROM referrals
WHERE pr.profile_id = r.[referrer_id(same as profile id)]
);
The alternative is to use a LEFT JOIN
to referrals
and simply check that no match is made: 另一种方法是使用LEFT JOIN
进行referrals
,只需检查是否匹配:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.