我有3张表如下。
salesman(sid,sname)
location(lid,lname)
sales_loc(sid,lid)
现在我要打印已经访问过所有地点的销售员的sid和sname。 我想要一个SQL查询,我不想要PL / SQL代码。
答案 0 :(得分:7)
select sid, sname from salesman
where not exists
(select 1 from location
where not exists
(select 1 from sales_loc
where sid=saleman.sid
and lid = location.lid));
答案 1 :(得分:4)
另一种方法:
select sid, sname from salesman
where
(select count(*) from location) =
(select count(*) from sales_loc where sales_loc.sid = salesman.sid)
修改强>
如果sales_loc(sid,lid)
对不是键,则以下查询更合适,
正如ammoQ建议:
select sid, sname from salesman
where
(select count(*) from location) =
(select count(distinct lid) from sales_loc where sales_loc.sid = salesman.sid)
答案 2 :(得分:0)
获取所有销售人员记录,其中访问的位置数等于位置数。
select sm.* from salesman as sm
where (select count(sl.*) from sales_loc as sl where sl.sid = sm.sid)
= (select count(l.*) from location as l);
答案 3 :(得分:0)
还有一个锅!
鉴于这些推销员及其领土......
SQL> select s.sname, l.lname
2 from salesman s
3 , location l
4 , sales_loc sl
5 where sl.sid = s.sid
6 and sl.lid = l.lid
7 order by s.sid, l.lid
8 /
SNAME LNAME
---------- ----------
FOX TRAIN
FOX BOAT
KNOX BOAT
KNOX HOUSE
SAM TRAIN
SAM BOAT
SAM HOUSE
7 rows selected.
SQL>
...此查询提取已访问过所有人的人...
SQL> select s.sname
2 from salesman s
3 where s.sid not in (
4 select sid from (
5 select cjs.sid, cjl.lid
6 from salesman cjs
7 cross join location cjl
8 minus
9 select sl.sid, sl.lid
10 from sales_loc sl
11 )
12 )
13 /
SNAME
------ ----
SAM
SQL>
答案 4 :(得分:0)
这应该有效。我试过了。
select a.sid, a.salesman, count(a.sid) as total from salesman a
inner join sales_loc b on b.sid = a.sid
inner join location c on c.lid = b.lid
group by a.sid, a.salesman
having count(a.sid) = (select count(*) from location)
它使用要比较的位置总数。
答案 5 :(得分:0)
推销员(SID,SNAME) 位置(盖,L-NAME) sales_loc(SID,盖子)
从salesman s,location l,sales_loc sl中选择s.sid,s.sname 其中s.sid = sl.sid 和l.lid = sl.lid