来自单个表的SQL查询

时间:2013-09-11 09:00:17

标签: mysql sql database

我想做两件事,但我已经卡住了。

首先我想写一个 SQL 声明,向我展示每项运动的所有运动和所有比赛日。表Event看起来像这样

Competitor ID  sports   branch         distance   men's/women    
  1            Running  Long-Distance  50000        MEN          

我还有一个名为EventDay的表格,其中包含CompetitorIDdatearenaspectators

CompetitorID  Date       Arena      Spectators
1             08/11/11   Olympus    500

这里显然会有一个加入声明,但我无法让它发挥作用。

在下面的表格中,我想了解一下这些国家从分支田径运动中获得的金牌奖牌。我也希望将它划分为性别,并按国家名称排序。

Land         Sex           Ammount gold
USA           L                   2
USA           M                   4
Ryss          L                   3
Ryss          H                   2

3 个答案:

答案 0 :(得分:1)

尝试这个

SELECT  Country,Sex
,       sum(AmountGold) as TotalGold
FROM YourTable
GROUP BY 
        Country,Sex
ORDER BY 
        Country,Sex

我认为这就是你需要的

答案 1 :(得分:0)

select  Country
,       Sex
,       sum(case when MedalType = 'Gold' then 1 else 0 end) as AmountGold
from    YourTable
group by
        Country
,       Sex
order by
        AmountGold desc

答案 2 :(得分:0)

试试这个:

SELECT COUNTRY, 
       SUM(AMOUNT_GOLD) AS TOTAL_GOLD 
FROM   ATHLETICS 
GROUP  BY LAND, 
          SEX 
ORDER  BY LAND