SQL where 限制

发布于 2024-12-19 22:21:28 字数 1023 浏览 1 评论 0原文

以下是我的输出,我只想显示那些重复的名称。每个名字都是一名飞行员,号码是飞行员驾驶的飞机类型。我想显示驾驶不止一架飞机的飞行员的姓名。我正在使用 sql*plus

PIL_PILOTNAME                  DEP_EQUIP_NO 
------------------------------ ------------ 
Gladchuk, Chet                 1085
Gladchuk, Chet                 1345
Green, Edward L.               1489
Harris, Kenneth C.             1253
Leiss, Ernst L.                1194
Long, Stuart                   1081
Pasewark, William B.           1026
Pasewark, William B.           1347
Scamell, Richard               1368

我的输出应该只显示:

PIL_PILOTNAME                  DEP_EQUIP_NO 
------------------------------ ------------ 
Gladchuk, Chet                 1085
Gladchuk, Chet                 1345
Pasewark, William B.           1026
Pasewark, William B.           1347

这是我的查询:

select distinct pil_pilotname,dep_equip_no from pilots  join departures
on dep_pilot_id = pil_pilot_id  join equip_type
on eq_equip_no = dep_equip_no
order by pil_pilotname;

Below is my output, I want to display only those names that repeat. Each name is a pilot and the number is the type of airplane the pilot fly's. I want to display the names of pilots who fly more than one airplane. I am using sql*plus

PIL_PILOTNAME                  DEP_EQUIP_NO 
------------------------------ ------------ 
Gladchuk, Chet                 1085
Gladchuk, Chet                 1345
Green, Edward L.               1489
Harris, Kenneth C.             1253
Leiss, Ernst L.                1194
Long, Stuart                   1081
Pasewark, William B.           1026
Pasewark, William B.           1347
Scamell, Richard               1368

My output should only display:

PIL_PILOTNAME                  DEP_EQUIP_NO 
------------------------------ ------------ 
Gladchuk, Chet                 1085
Gladchuk, Chet                 1345
Pasewark, William B.           1026
Pasewark, William B.           1347

Here's my query:

select distinct pil_pilotname,dep_equip_no from pilots  join departures
on dep_pilot_id = pil_pilot_id  join equip_type
on eq_equip_no = dep_equip_no
order by pil_pilotname;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

书信已泛黄 2024-12-26 22:21:29
select pil_pilotname, dep_equip_no
from pilots
inner join departures d on dep_pilot_id = pil_pilot_id
inner join equip_type on eq_equip_no = dep_equip_no
where pil_pilotname in (
  select pil_pilotname 
  from pilots
  inner join departures d on dep_pilot_id = pil_pilot_id
  inner join equip_type on eq_equip_no = dep_equip_no
  group by pil_pilotname
  having count(*) > 1)
order by pil_pilotname;

这会给你你所需要的。 where 中的内部 select 进行过滤。

select pil_pilotname, dep_equip_no
from pilots
inner join departures d on dep_pilot_id = pil_pilot_id
inner join equip_type on eq_equip_no = dep_equip_no
where pil_pilotname in (
  select pil_pilotname 
  from pilots
  inner join departures d on dep_pilot_id = pil_pilot_id
  inner join equip_type on eq_equip_no = dep_equip_no
  group by pil_pilotname
  having count(*) > 1)
order by pil_pilotname;

This will give you what you need. The inner select in the where does the filtering.

紫瑟鸿黎 2024-12-26 22:21:28

在这种情况下,最好的选择是使用分析

select PIL_PILOTNAME, DEP_EQUIP_NO
from (
        select pil_pilotname,dep_equip_no, count(*) over (partition by pil_pilot_id) as cnt
        from pilots
            join departures on (dep_pilot_id = pil_pilot_id)
            join equip_type on (eq_equip_no = dep_equip_no)
    )
where cnt > 1
order by pil_pilotname;

In this case the best option would be to use analytics

select PIL_PILOTNAME, DEP_EQUIP_NO
from (
        select pil_pilotname,dep_equip_no, count(*) over (partition by pil_pilot_id) as cnt
        from pilots
            join departures on (dep_pilot_id = pil_pilot_id)
            join equip_type on (eq_equip_no = dep_equip_no)
    )
where cnt > 1
order by pil_pilotname;
深海蓝天 2024-12-26 22:21:28

这两个(或两个)之一应该有效

select distinct pil_pilotname, dep_equip_no, count(*) as c from pilots
join departures on dep_pilot_id = pil_pilot_id  
join equip_type on eq_equip_no = dep_equip_no
group by pil_pilotname
where c > 1
order by pil_pilotname;

或者

select distinct p1.pil_pilotname, p1.dep_equip_no from pilots p1
join departures d1 on d1.dep_pilot_id = p1.pil_pilot_id  
join equip_type e1 on e1.eq_equip_no = d1.dep_equip_no
where exists (
    select distinct p2.pil_pilotname from pilots p2
    join departures d2 on d2.dep_pilot_id = p2.pil_pilot_id  
    join equip_type e2 on e2.eq_equip_no = d2.dep_equip_no
    where p1.dep_equip_no != p2.dep_equip_no
)
order by p1.pil_pilotname;

One of these two (or both) should work

select distinct pil_pilotname, dep_equip_no, count(*) as c from pilots
join departures on dep_pilot_id = pil_pilot_id  
join equip_type on eq_equip_no = dep_equip_no
group by pil_pilotname
where c > 1
order by pil_pilotname;

Or

select distinct p1.pil_pilotname, p1.dep_equip_no from pilots p1
join departures d1 on d1.dep_pilot_id = p1.pil_pilot_id  
join equip_type e1 on e1.eq_equip_no = d1.dep_equip_no
where exists (
    select distinct p2.pil_pilotname from pilots p2
    join departures d2 on d2.dep_pilot_id = p2.pil_pilot_id  
    join equip_type e2 on e2.eq_equip_no = d2.dep_equip_no
    where p1.dep_equip_no != p2.dep_equip_no
)
order by p1.pil_pilotname;
瀞厅☆埖开 2024-12-26 22:21:28

您可以使用GROUP BY 子句。

select distinct pil_pilotname,dep_equip_no, COUNT(*) AS FlightCount from pilots  join departures
on dep_pilot_id = pil_pilot_id  join equip_type
on eq_equip_no = dep_equip_no
group by pil_pilotname
having FlightCount > 1;

You can make use of a GROUP BY clause.

select distinct pil_pilotname,dep_equip_no, COUNT(*) AS FlightCount from pilots  join departures
on dep_pilot_id = pil_pilot_id  join equip_type
on eq_equip_no = dep_equip_no
group by pil_pilotname
having FlightCount > 1;
凉世弥音 2024-12-26 22:21:28
select  pil_pilotname,dep_equip_no from pilots,departures,equip_type
where dep_pilot_id = pil_pilot_id  and eq_equip_no = dep_equip_no
group by pil_pilotname
having count(pil_pilotname) >1; 
select  pil_pilotname,dep_equip_no from pilots,departures,equip_type
where dep_pilot_id = pil_pilot_id  and eq_equip_no = dep_equip_no
group by pil_pilotname
having count(pil_pilotname) >1; 
别想她 2024-12-26 22:21:28

似乎您没有从 equip_type 表中提取任何内容,在这种情况下,您可能不需要加入它。我在这里假设 dep_equip_noequip_type.eq_equip_no 的外键,因此无法保存 equip_type 中找不到的值。

考虑到这一点,我的解决方案将如下所示:

SELECT DISTINCT
  p.pil_pilotname,
  d.dep_equip_no
FROM pilots p
  INNER JOIN (
    SELECT dep_pilot_id
    FROM departures
    GROUP BY dep_pilot_no
    HAVING COUNT(DISTINCT dep_equip_no) > 1
  ) s ON p.pil_pilot_id = s.dep_pilot_id
  INNER JOIN departures d ON p.pil_pilot_id = d.dep_pilot_id

基本上,除了删除与 equip_type 的联接之外,我只向返回 dep_pilot_id 列表的子选择添加了一个联接具有多个不同关联 dep_equip_no 的值。

如果您认为查询中需要 equip_type 表,您可以将删除的联接放回去,它应该不会影响结果。

Seems like you are not pulling anything from the equip_type table, in which case you might not need to join to it. I'm assuming here that dep_equip_no is a foreign key to equip_type.eq_equip_no and thus cannot hold values not found in equip_type.

With that in mind, my solution would look like this:

SELECT DISTINCT
  p.pil_pilotname,
  d.dep_equip_no
FROM pilots p
  INNER JOIN (
    SELECT dep_pilot_id
    FROM departures
    GROUP BY dep_pilot_no
    HAVING COUNT(DISTINCT dep_equip_no) > 1
  ) s ON p.pil_pilot_id = s.dep_pilot_id
  INNER JOIN departures d ON p.pil_pilot_id = d.dep_pilot_id

Basically, apart from removing the join to equip_type, I only added one join to a subselect that returns a list of dep_pilot_id values having more than one distinct associated dep_equip_no.

If you think you need the equip_type table in your query, you can put the removed join back, it should not affect the results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文