2011-08-11 157 views
2

我确定有一个简单的解决方案,但我根本找不到它。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!

回答

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

WHERE需要在子查询中替换HAVING。 – adamcodes

1

它是否如你预期的工作?

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

此外,尝试删除DISTINCT关键字,如果你需要为每个不同的状态

+0

这不太可能工作 – ajreal

+0

可能是,它会返回COUNTRY - STATE - NUMBER_OF_COUNTRIES - NUMBER_OF_STATE_PER_COUNTRY,通过移除DISTING国家,您可能会获得更多的扩展视图 – sll

+0

这将始终返回1为国家,因为'country-state'中的每个记录是总是有相同的国家 – 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

算同一个国家我的解决方案,测试,应该给予确切的结果:

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

为避免必须重复查询表格两次,可以使用分析。

​​