1

标题有点绕。这是一个具体的例子。我有两张桌子:

+-------------+--------------------------------+------+-----+---------+-------+
| Field       | Type                           | Null | Key | Default | Extra |
+-------------+--------------------------------+------+-----+---------+-------+
| event       | varchar(100)                   | NO   | MUL | NULL    |       |
| sport       | varchar(100)                   | NO   |     | NULL    |       |
| athleteCode | char(10)                       | NO   | MUL | NULL    |       |
| medal       | enum('GOLD','SILVER','BRONZE') | NO   |     | NULL    |       |
+-------------+--------------------------------+------+-----+---------+-------+

+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name    | varchar(100) | NO   |     | NULL    |       |
| code    | char(10)     | NO   | PRI | NULL    |       |
| country | varchar(100) | NO   | MUL | NULL    |       |
+---------+--------------+------+-----+---------+-------+

第一桌是奖牌桌。第二张桌子是运动员桌。这两个表通过medals.athleteCode和关联athlete.code。我希望能够列出显示以下信息的查询:

COUNTRY | GOLD | SILVER | BRONZE | TOTAL

到目前为止,我能够做到的唯一方法是使用此查询:

SELECT country, medal, COUNT(medal) as count  
    FROM athletes, medals  
    WHERE athletes.code=medals.athleteCode  
    GROUP BY country, medal  
    ORDER BY country, medal;

但是在我完成这个查询之后,我仍然需要处理这个查询(通过 PHP),因为这只会让我得到每个国家的每种奖牌类型(即所有中国的金牌、所有中国的银牌、所有中国的铜牌等)。有没有办法创建一个查询,其中查询的每条记录(即行)是:COUNTRY | GOLDS | SILVERS | BRONZES | TOTAL?我看了看,COUNT()但我不确定如何使用它。


2012.08.17

@"habib zare" 的解决方案非常接近。这是我的调整:

SELECT country, 
(SELECT count(*) FROM medals WHERE a.code=m.athleteCode AND medal='Gold') AS Gold,
(SELECT count(*) FROM medals WHERE a.code=m.athleteCode AND medal='Silver') AS Silver,
(SELECT count(*) FROM medals WHERE a.code=m.athleteCode AND medal='Bronze') AS Bronze,
(SELECT count(*) FROM medals WHERE a.code=m.athleteCode) AS Total
FROM medals m JOIN athletes a
ON m.athleteCode=a.code
GROUP BY country
ORDER BY country, Gold DESC, Silver DESC, Bronze DESC

问题是二级SELECT报表需要根据国家来选择;那就是我需要类似的东西:

SELECT country AS Country,
(SELECT count(*) FROM medals WHERE a.code=m.athleteCode AND medal='Gold' AND a.country=Country) AS Gold,
4

3 回答 3

0

我不明白你想要什么,但我认为你想要这个:

奖牌:奖牌活动体育运动员代码金牌 jh 游泳 1

青铜 dfg ert 2

黄金 fg 错误 1

银作为 erf 4

铜牌 erf 5

金 df dfg 6

黄金 sdds tekvando 3

青铜 df jh 1

青铜 yy jh 1

银 ik 为 1

银 shj jsg 3

银 shj jsg 5

银 sjdk hgj 5

银 wuytu wopow 5

银 wuytu wopow 6

银 wuytu wopow 6

运动员:

名称代码国家

哈比卜 1 伊朗

艾哈迈德 2 阿塞拜疆

穆罕默德 3 火鸡

所以 4 伊朗

ghg 5 阿塞拜疆

yewtuuy 6 阿塞拜疆

我的查询:

SELECT country,name,
(select count(*) from medals where athleteCode=m.athleteCode and medal='Gold') as Gold,
(select count(*) from medals where athleteCode=m.athleteCode and medal='Silver') as Silver,
(select count(*) from medals where athleteCode=m.athleteCode and medal='Bronze') as Bronze,
(select count(*) from medals where athleteCode=m.athleteCode) as Total
FROM medals m join athletes a
on m.athleteCode=a.code
group by country,code
order by country,Gold desc,Silver desc,Bronze desc

结果 :

国名 金银铜 合计

阿塞拜疆 yewtuuy 1 2 0 3

阿塞拜疆 GHG 0 3 1 4

阿塞拜疆艾哈迈德 0 0 1 1

伊朗哈比卜 2 1 2 5

伊朗所以 0 1 0 1

土耳其穆罕默德 1 1 0 2

于 2012-08-17T16:07:36.577 回答
0

在 MS SQL ServerSUM(CASE WHEN medal = 'GOLD' THEN 1 ELSE 0 END) AS GOLDS中可以与GROUP BY country.

于 2012-08-17T04:09:58.717 回答
0

如果你使用 MSSQL,你也可以使用 OUTER APPLY:

样本:

SELECT Country
  ,Gold = ISNULL(GoldMedals.MedalCount,0)
  ,Total = ISNULL(TotalMedals.MedalCount,0) 
FROM athletes
  OUTER APPLY (
     SELECT MedalCount = COUNT(*)
     FROM medals
     WHERE medals.athletescode = atheletes.medalsathleteCode
       AND medals.Type = 'Gold'
   ) GoldMedals
   OUTER APPLY (
     SELECT MedalCount = COUNT(*)
     FROM medals
     WHERE medals.athletescode = atheletes.medalsathleteCode
    ) TotalMedals   
GROUP BY country

您可能需要添加 SUM 而不是 ISNULL 但我并没有真正测试此代码,所以按原样使用它......

于 2012-08-17T04:16:27.720 回答