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 per provare e googling. 'jsut cambia' a.country' in 'b.country'. – xQbert