Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 排除嵌套SQL查询的结果_Mysql_Sql - Fatal编程技术网

Mysql 排除嵌套SQL查询的结果

Mysql 排除嵌套SQL查询的结果,mysql,sql,Mysql,Sql,我有一张优胜者对失败者的表格 +----+--------+-------+ | ID | Winner | Loser | +----+--------+-------+ | 1 | 2 | 3 | | 2 | 1 | 2 | +----+--------+-------+ 在项目1和项目2之间的最近一场比赛中,1赢了ID 2。在本例中,我将其称为当前赢家和当前输家 我正在尝试构建一个查询,它可以根据过去的结果进行推断 e、 g

我有一张优胜者对失败者的表格

+----+--------+-------+  
| ID | Winner | Loser |  
+----+--------+-------+  
|  1 |      2 |     3 |  
|  2 |      1 |     2 |  
+----+--------+-------+  
在项目1和项目2之间的最近一场比赛中,1赢了ID 2。在本例中,我将其称为当前赢家和当前输家

我正在尝试构建一个查询,它可以根据过去的结果进行推断

e、 g.如果2>3和1>2。然后我需要记录1>3的值

我正在构建的查询将找到多个推断出的输家与当前的赢家

理想的查询将返回一个失败者数组,我可以循环遍历该数组并将其作为推断结果记录在表中。在本例中为3

该表将更新为:

 +----+--------+-------+
 | ID | Winner | Loser |
 +----+--------+-------+
 |  1 |      1 |     2 |
 |  2 |      2 |     3 |
 |  3 |      1 |     3 |
 +----+--------+-------+
如果再次运行查询,它将不会返回任何结果

到目前为止,我的过程是:

查找当前失败者的所有内容,从以前的失败者到当前的失败者 检查表格,看看以前的输家和现在的输家中是否有人曾经和现在的赢家比赛过。 任何以前的失败者都应该被移除 要获得当前失败者击败的事物列表,我使用:

    select * from TABLE1 where winner = 2
对于第二个要点,我有两个嵌套查询:

    select * from TABLE1 where winner = 1 and loser = (select loser from rp_poss where winner = 2)
    select * from TABLE1 where loser = 1 and winner = (select loser from rp_poss where winner = 2)
我真的不知道如何把这些放在一起,删除我不想要的行。有人能告诉我什么是最好的,最有效的查询吗?例如,嵌套查询,某种连接?Pea brain真的在努力解决这个问题


提前感谢

您可以这样做,通过显式地查找两个项目之间匹配的特定记录,并计数以查看它们是否为零

CURRENTLOSER和CURRENTWINNER是变量或其他内容的占位符

select previous.loser
from table1 previous
where previous.winner=CURRENTLOSER and (
   select count(*)
   from table1 ancient
   where (ancient.winner=CURRENTWINNER and ancient.loser=previous.loser) or
         (ancient.loser=CURRENTWINNER and ancient.winner=previous.loser)
   ) = 0

对表1中的表格进行别名处理将有助于在您的头脑中清楚地了解算法。

这将为每个人和竞争对手获得一行,以及该竞争对手的最后一个结果:即,如果1人与2人交锋失败,然后再次与2人交锋并获胜,此查询将显示竞争对手2获胜的人员1,以及竞争对手1失败的人员2。它显示每个竞争对手相对于个人的最新结果


下面的查询将在第一次运行时为1和2之间的最近匹配插入推断的输家。第二次它不会插入任何新行

最初,“不存在”子查询的where id
insert into mytable (winner, loser)
select current.winner, previous.loser
from (select id, winner, loser
    from mytable where 
    (winner = 1 and loser = 2)
    or (winner = 2 and loser = 1)
    order by id desc limit 1) current
join mytable previous 
    on previous.winner = current.loser
    and previous.id < current.id
where not exists (select 1 from mytable
    where id <> current.id 
    and ((winner = current.winner and loser = previous.loser)
    or (winner = previous.loser and loser = current.winner)))

我不够聪明,不知道这句话是什么意思means@ItayMoav-我也不明白。OP询问如何在SQL中执行此操作,因此,如果您的答案是他应该使用存储过程,那么这并不超出问题所寻求的指导原则。只要它包括如何做。这是一个完全合法的SQL任务-不需要涉及外部语言。太好了!这很有魅力!非常喜欢使用计数忽略行的概念。从FuzzyTree的回答中注意到,where not exists可能是一种比where…count=0更惯用的表达方式。
insert into mytable (winner, loser)
select current.winner, previous.loser
from (select id, winner, loser
    from mytable where 
    (winner = 1 and loser = 2)
    or (winner = 2 and loser = 1)
    order by id desc limit 1) current
join mytable previous 
    on previous.winner = current.loser
    and previous.id < current.id
where not exists (select 1 from mytable
    where id <> current.id 
    and ((winner = current.winner and loser = previous.loser)
    or (winner = previous.loser and loser = current.winner)))