2013-05-22 17 views
8

Ho bisogno di usare Self Join su questo tavolo.MySql. Come usare Self Join

+------------+------+--------+ 
| Country | Rank | Year | 
+------------+------+--------+ 
|France  | 55 | 2000 | 
+------------+------+--------+ 
|Canada  | 30 | 2000 | 
+------------+------+--------+ 
|Liberia  | 59 | 2001 | 
+------------+------+--------+ 
|Turkey  | 78 | 2000 | 
+------------+------+--------+ 
|Japan  | 65 | 2003 | 
+------------+------+--------+ 
|Romania  | 107 | 2001 | 
+------------+------+--------+ 

Ho bisogno di usare il self join per ottenere quali paesi ha lo stesso anno della Turchia. Visualizza solo il Paese e l'anno.

Questo è quello che sto cercando di fare.

SELECT DISTINCT a.Country, a.Year 
FROM table1 AS a, table1 AS b 
WHERE a.Year=b.Year and a.Country='Turkey'; 

^google si unisce e ce l'ha fatta.

Sto ricevendo solo la Turchia. Che cosa sto facendo di sbagliato?

+2

+1 per provare e googling. 'jsut cambia' a.country' in 'b.country'. – xQbert

risposta

10

Sei così vicino!

Poiché si dice che si sta visualizzando il paese e l'anno da A e che si limita a A. Country della Turchia, la Turchia è tutto ciò che vedrete. È necessario modificare i selettori da B.country e B.year oppure modificare la clausola where in B.country.

Questo sta usando un cross join che diventerà più lento più i record ci sono in una tabella.

SELECT DISTINCT b.Country, b.Year 
FROM table1 AS a, 
    table1 AS b 
WHERE a.Year=b.Year 
    and a.Country='Turkey'; 

potrebbe essere scritto come ... e probabilmente avrà lo stesso piano di esecuzione.

SELECT DISTINCT b.Country, b.Year 
FROM table1 AS a 
CROSS JOIN table1 AS b 
WHERE a.Year=b.Year 
    and a.Country='Turkey'; 

O Questo utilizza un INNER JOIN che limita il lavoro del motore deve fare e non soffrono di degrado delle prestazioni che un cross join sarebbe.

SELECT DISTINCT a.Country, a.Year 
FROM table1 AS a 
INNER JOIN table1 AS b 
WHERE a.Year=b.Year 
    and b.Country='Turkey'; 

PERCHE ':

in considerazione ciò che il motore SQL farà quando l'unione si verifica AB

+------------+------+--------+------------+------+--------+ 
| A.Country | Rank | Year | B.Country | Rank | Year | 
+------------+------+--------+------------+------+--------+ 
|France  | 55 | 2000 |France  | 55 | 2000 | 
+------------+------+--------+------------+------+--------+ 
|Canada  | 30 | 2000 |France  | 55 | 2000 | 
+------------+------+--------+------------+------+--------+ 
|Turkey  | 78 | 2000 |France  | 55 | 2000 | 
+------------+------+--------+------------+------+--------+ 
|France  | 55 | 2000 |Canada  | 30 | 2000 | 
+------------+------+--------+------------+------+--------+ 
|Canada  | 30 | 2000 |Canada  | 30 | 2000 | 
+------------+------+--------+------------+------+--------+ 
|Turkey  | 78 | 2000 |Canada  | 30 | 2000 | 
+------------+------+--------+------------+------+--------+ 
|France  | 55 | 2000 |Turkey  | 78 | 2000 | 
+------------+------+--------+------------+------+--------+ 
|Canada  | 30 | 2000 |Turkey  | 78 | 2000 | 
+------------+------+--------+------------+------+--------+ 
|Turkey  | 78 | 2000 |Turkey  | 78 | 2000 | 
+------------+------+--------+------------+------+--------+ 

Così, quando hai detto visualizzazione A.Country e A.Year dove A.Country è la Turchia, è possibile vedere tutto può tornare è la Turchia (a causa del distinto solo 1 record)

Ma se si do B.Country è la Turchia e visualizza A.Country, avrai Francia, Canada e Turchia!

+1

Grazie, b4 non sapevo perché a. e B. era li. ora faccio :) – hank99

5

Change a.Country = 'Turkey'-b.Country = 'Turkey'

Hai SELECT DISTINCT a.Country, ma la sua condizione è a.Country = 'Turkey'. Anche se ottieni più righe, vengono filtrate dal numero DISTINCT

+0

Grazie mille – hank99

+0

rispondi a questa [domanda] (http://dba.stackexchange.com/q/101947/66799) – Rembo

0
select distinct country,year from table1 where year=(select year from table 
where country='turkey') and country !=turkey;