Domanda

I have one table with the following fields:

battle_id, winner, looser  
 1  200    44  
 2   55   366  
 3   44   200  
 4  123   200  
 5  200    44  
 6   55   366  
 7  177   205  
 8  188   211  
 9  366    55  
10   55   366

right now it has about 1300 records (its small), and there are about 400 players, in each battle there can only be a winner and a looser (there are no draws)

how can i find all the repeated battles? i do not want to find all the repeated battles of one player, i do want to know all the repeated battles of all the players...i know that i cam make a recursive function in php that iterates over all the battles and assign them to a matrix, but just for fun...is there a way to do it only on mysql?

And how can i optimize the table to find the repeated battles more quickly?

regards

EDIT:

For example i want the query to show:

battle_id, winner, looser  
 1  200    44  
 2   55   366  
 3   44   200  
 5  200    44  
 6   55   366  
 9  366    55  
10   55   366
È stato utile?

Soluzione

This should work, using a self-join could result in many duplicated entries

SQLFIDDLE

SELECT
    t1.battle_id,
    t1.winner,
    t1.loser
FROM
    your_table t1
WHERE
    EXISTS (
             SELECT
                 1
             FROM
                 your_table t2
             WHERE
                 ( ( t1.winner = t2.winner
                 AND t1.loser = t2.loser )
                 OR ( t1.loser = t2.winner
                 AND t1.winner = t2.loser ) )
                 AND t1.battle_id <> t2.battle_id
    )

Altri suggerimenti

try this:

SELECT b1.battle_id,
       b1.winner,
       b1.looser 
FROM   battles as b1
group  by b1.battle_id, b1.winner,  b1.looser 
having count(*)>=2
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top