如何 select 这些从未输过比赛的拳击手的姓氏?
How to select last names of these boxers that never lost a fight?
我知道我的问题很奇怪,但我不知道如果我想查看所有从未输过比赛的拳击手姓氏,select 应该如何查看 oracle。
所以我有:
- table "boxer" 列:id、fname、lname、weight。
- table "fight" 带有来自 table 拳击手的两个外键(id_boxer1 和 id_boxer2)和一个列获胜者(如果 boxer1 获胜则获胜者将是 1 号,如果 boxer2 获胜则获胜者将是 2 号)
我如何 select 在 oracle 中永不输掉战斗的拳击手?
我有 oracle 11g 快捷版。 (不,我不能更新,我的教授说我的答案应该适用于 11g)。
很抱歉,如果我的问题不符合本页的条件。
编辑:
感谢您的帮助,我实现了这一目标:
select b.lname
from boxer b
where
not exists (
select 1
from fight f
where (f.id_boxer1 = b.id and winner = 2) or (f.id_boxer2 = b.id and winner = 1)
)
and exists (
select 1
from fight f
where (f.id_boxer1 = b.id and winner = 1) or (f.id_boxer2 = b.id and winner = 2)
)
and not exists (
select 1
from fight f
where (f.id_boxer1 = b.id and winner = 0) or (f.id_boxer2 = b.id and winner= 0)
)
您可以使用不存在的:
select b.*
from boxer b
where not exists (
select 1
from fight f
where (f.id_boxer_1 = b.id and f.winner = 2) or (f.id_boxer_2 = b.id and f.winner = 1)
)
如果您想要拳击手至少打过一次并且从未输过:
select b.*
from boxer b
where
not exists (
select 1
from fight f
where (f.id_boxer_1 = b.id and f.winner = 2) or (f.id_boxer_2 = b.id and f.winner = 1)
)
and exists (
select 1
from fight f
where (f.id_boxer_1 = b.id and f.winner = 1) or (f.id_boxer_2 = b.id and f.winner = 2)
)
我会使用 case
表达式从 fight
table 中获取失败的拳击手,然后使用 boxer
table 查询 boxer
table not exists
运算符:
SELECT *
FROM boxer b
WHERE NOT EXIST (SELECT *
FROM fight
WHERE b.id = CASE winner WHEN 1 THEN id_boxer2
WHEN 2 THEN id_boxer1
END)
注意 - 如果 winner=0
(即两个拳击手都未获胜),则 can 表达式将为 return null
,它永远不会等于 b.id
.
我知道我的问题很奇怪,但我不知道如果我想查看所有从未输过比赛的拳击手姓氏,select 应该如何查看 oracle。
所以我有:
- table "boxer" 列:id、fname、lname、weight。
- table "fight" 带有来自 table 拳击手的两个外键(id_boxer1 和 id_boxer2)和一个列获胜者(如果 boxer1 获胜则获胜者将是 1 号,如果 boxer2 获胜则获胜者将是 2 号)
我如何 select 在 oracle 中永不输掉战斗的拳击手? 我有 oracle 11g 快捷版。 (不,我不能更新,我的教授说我的答案应该适用于 11g)。
很抱歉,如果我的问题不符合本页的条件。
编辑:
感谢您的帮助,我实现了这一目标:
select b.lname
from boxer b
where
not exists (
select 1
from fight f
where (f.id_boxer1 = b.id and winner = 2) or (f.id_boxer2 = b.id and winner = 1)
)
and exists (
select 1
from fight f
where (f.id_boxer1 = b.id and winner = 1) or (f.id_boxer2 = b.id and winner = 2)
)
and not exists (
select 1
from fight f
where (f.id_boxer1 = b.id and winner = 0) or (f.id_boxer2 = b.id and winner= 0)
)
您可以使用不存在的:
select b.*
from boxer b
where not exists (
select 1
from fight f
where (f.id_boxer_1 = b.id and f.winner = 2) or (f.id_boxer_2 = b.id and f.winner = 1)
)
如果您想要拳击手至少打过一次并且从未输过:
select b.*
from boxer b
where
not exists (
select 1
from fight f
where (f.id_boxer_1 = b.id and f.winner = 2) or (f.id_boxer_2 = b.id and f.winner = 1)
)
and exists (
select 1
from fight f
where (f.id_boxer_1 = b.id and f.winner = 1) or (f.id_boxer_2 = b.id and f.winner = 2)
)
我会使用 case
表达式从 fight
table 中获取失败的拳击手,然后使用 boxer
table 查询 boxer
table not exists
运算符:
SELECT *
FROM boxer b
WHERE NOT EXIST (SELECT *
FROM fight
WHERE b.id = CASE winner WHEN 1 THEN id_boxer2
WHEN 2 THEN id_boxer1
END)
注意 - 如果 winner=0
(即两个拳击手都未获胜),则 can 表达式将为 return null
,它永远不会等于 b.id
.