2011-08-11 7 views
2

Imは必ず参加します。MySQLはそこにこれに対する簡単な解決策はありますが、私は全くそれを把握することはできませんクエリ

私はそれらに関連付けられている州と国を持っている写真のテーブルを持っています。

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 | 
+-------------+---------------------+----------+ 

私が好きなもの:他の状態ごとの写真(国)の数を取得します

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 | 
+-------------+----------+ 

: 私は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!

答えて

0

これはトリックを行う必要があります。

SELECT country_state.country, country_state.state, 
     country_state.statet, country.countryt 
FROM 
(SELECT country, count(*) as countryt FROM photos group by country) AS country 
JOIN 
(SELECT country, state, count(*) as statet FROM photos group by country, state) AS country_state ON country.country = country_state.country 
0

は個人的に

select * 
from (
    select country, state, count(*) from photos group by country, state 
) a 
inner join (
    select country, count(*) from photos group by country 
) b on a.country = b.country 
1

を試してみてください、私はサブクエリ行きたい:

select 
    country, 
    state, 
    count(*) as state, 
    (select count(*) from photos inr 
      where inr.country = otr.country) 
from photos otr group by country, state; 
+0

サブクエリでHAVINGを置き換える必要がある場所。 – adamcodes

1

それはあなたが期待通りに動作していますか?

SELECT country, state, COUNT(DISTINCT country), COUNT(state) 
    FROM photos 
    GROUP BY country, state 

また、テストされ、私のソリューションあなたは異なる状態ごとに同じ国をカウントする必要がある場合

+0

これはうまくいきません – ajreal

+0

それはCOUNTRY - STATE - NUMBER_OF_COUNTRIES - NUMBER_OF_STATE_PER_COUNTRYを返します。さらに拡大表示を得ることができるDISTING国を削除します。 – sll

+0

'country-state'のすべてのレコードは、国のために常に1を返します。常に絶対に同じ国 – ajreal

1
select cs.country, cs.state, count(*) as statet, c.countryt 
from photos cs 
left join (select country, count(*) countryt from photos group by country) as c 
on c.country=cs.country 
group by cs.country, cs.state; 
0

DISTINCTキーワードを削除しようとすると、正確な結果を与える必要があります:

SELECT a.country AS country, p.state AS state, COUNT(*) AS statet, a.count AS countryt 
FROM (SELECT p1.country, COUNT(*) AS count FROM photos p1 GROUP BY p1.country) a 
JOIN photos p ON (p.country = a.country) 
GROUP BY p.state; 

を楽しい!あなたが分析を使用することができ、二回あなたのテーブルを照会することを避けるために

0

​​
関連する問題