2

表 A:用户名

表 B: accid userid accno

表c:loanid userid amt

嗨 frnds,我想从表 A 中获取客户的姓名,从表 B 中获取 accno 并且它们不包含在表 c 中。帮我

4

3 回答 3

2
SELECT a.name, b.accno
FROM a inner join b on b.userid = a.userid
where a.userid not in (select distinct userid from c)

我认为这个查询怎么样?

于 2012-12-14T10:57:07.450 回答
1

尝试:

SELECT a.name, b.accno
FROM tableA a 
JOIN tableB b on a.userid = b.userid
LEFT JOIN tableC c on a.userid = c.userid
WHERE c.userid IS NULL

进行左连接并检查返回的值是否为空,检查表 c 中是否没有表 a 和表 b 中的记录。

于 2012-12-14T08:23:42.093 回答
1

解决方案1:

;with cte as
(Select a.name,b.accno,a.userid
from tableA a
join tableB b
on a.userid = b.userid)

select x.name,x.accno
from cte x 
where x.userid not in (select userid from tableC)

解决方案2:

;with cte as
(Select a.name,b.accno,a.userid
from tableA a join tableB b 
on a.userid = b.userid)
select x.name,x.accno
from cte x 
where x.userid in(select c.userid 
                 from cte c 
                 except 
                 select tc.userid 
                from tableC tc)

解决方案 3

;with cte as
(Select a.name,b.accno,a.userid
from tableA a join tableB b 
on a.userid = b.userid)

select x.name,x.accno
from cte x
left join tableC tc
on x.userid = tc.userid
where tc.userid is null
于 2012-12-14T08:25:28.050 回答