我確定有一個簡單的解決方案,但我根本找不到它。Mysql加入查詢
我有一個照片的表格,其中有與他們相關的州和國家。 我有兩個簡單的查詢,一個讓每個國家的照片數量:
select country, count(*) from photos group by country;
+-------------+----------+
| country | count(*) |
+-------------+----------+
| Argentina | 6 |
| Australia | 5 |
| Chile | 3 |
| Ecuador | 10 |
| France | 1 |
| Hong Kong | 4 |
| Indonesia | 6 |
| Ireland | 16 |
| Malaysia | 1 |
| New Zealand | 3 |
| Peru | 8 |
+-------------+----------+
其他獲得的每個州的照片(和國家)的數量:
select country, state, count(*) from photos group by country, state;
+-------------+---------------------+----------+
| country | state | count(*) |
+-------------+---------------------+----------+
| Argentina | Misiones | 2 |
| Argentina | Salta | 4 |
| Australia | New South Wales | 1 |
| Australia | Victoria | 4 |
| Chile | Antofagasta | 3 |
| Ecuador | Galapagos | 5 |
| Ecuador | Sucumbios | 5 |
| France | Aquitaine | 1 |
| Hong Kong | Hong Kong | 4 |
| Indonesia | Bali | 3 |
| Indonesia | Nusa Tenggara Barat | 1 |
| Indonesia | Yogyakarta | 2 |
| Ireland | Antrim | 1 |
| Ireland | Cork | 1 |
| Ireland | Derry | 2 |
| Ireland | Donegal | 8 |
| Ireland | Kerry | 1 |
| Ireland | Sligo | 1 |
| Ireland | Waterford | 1 |
| Ireland | Wexford | 1 |
| Malaysia | Sabah | 1 |
| New Zealand | Manawatu | 2 |
| New Zealand | Westland | 1 |
| Peru | Cusco | 6 |
| Peru | La Libertad | 2 |
+-------------+---------------------+----------+
我想什麼做的是執行這些查詢導致以下聯接:
+-------------+---------------------+----------+----------+
| country | state | statet | countryt |
+-------------+---------------------+----------+----------+
| Argentina | Misiones | 2 | 6 |
| Argentina | Salta | 4 | 6 |
| Australia | New South Wales | 1 | 5 |
| Australia | Victoria | 4 | 5 |
| Chile | Antofagasta | 3 | 3 |
| Ecuador | Galapagos | 5 | 10 |
| Ecuador | Sucumbios | 5 | 10 |
| France | Aquitaine | 1 | 1 |
| Hong Kong | Hong Kong | 4 | 4 |
| Indonesia | Bali | 3 | 6 |
| Indonesia | Nusa Tenggara Barat | 1 | 6 |
| Indonesia | Yogyakarta | 2 | 6 |
| Ireland | Antrim | 1 | 16 |
| Ireland | Cork | 1 | 16 |
| Ireland | Derry | 2 | 16 |
| Ireland | Donegal | 8 | 16 |
| Ireland | Kerry | 1 | 16 |
| Ireland | Sligo | 1 | 16 |
| Ireland | Waterford | 1 | 16 |
| Ireland | Wexford | 1 | 16 |
| Malaysia | Sabah | 1 | 1 |
| New Zealand | Manawatu | 2 | 3 |
| New Zealand | Westland | 1 | 3 |
| Peru | Cusco | 6 | 8 |
| Peru | La Libertad | 2 | 8 |
+-------------+---------------------+----------+----------+
我一直在玩了一上午的連接和無法弄清楚,任何指針將是真正appre ciated!
WHERE需要在子查詢中替換HAVING。 – adamcodes