选择运动项目较少的5个运动学科

时间:2018-12-16 17:55:57

标签: mysql sql database postgresql

我有这个运动桌:

CREATE TABLE Sports(
    name_sport VARCHAR(50) PRIMARY KEY,
    categories_sport VARCHAR(50),
    type_result_sport VARCHAR(50),
    discipline TEXT,
    enEquipe BOOLEAN 
);

我充满了像这样的数据:

('Women''s 50m Freestyle','freestyle','aquatics','temps','FALSE'),

('Trap Women','shooting','shooting','temps','FALSE'),
('10m Air Pistol Men','shooting','shooting','score','FALSE'),
('10m Air Rifle Men','shooting','shooting','score','FALSE'),

('Women''s Discus Throw','throws','athletics','score','FALSE'),
('Women''s Hammer Throw','throws','athletics','score','FALSE'),
('Women''s Javelin Throw','throws','athletics','score','FALSE'),
('Men''s 100m','track','athletics','temps','FALSE'),

('Women''s Doubles','badminton','badminton','temps','TRUE'),
('Women''s Singles','badminton','badminton','temps','FALSE'),
('Men''s Doubles','badminton','badminton','temps','TRUE'),
('Men''s Singles','badminton','badminton','temps','FALSE'),

('Lightweight Women''s Double Sculls','rowing','rowing','temps','FALSE'),
('Women''s Double Sculls','rowing','rowing','temps','FALSE'),
('Women''s Eight','rowing','rowing','temps','FALSE'),
('Women''s Pair','rowing','rowing','temps','FALSE'),
('Women''s Quadruple Sculls','rowing','rowing','temps','FALSE'),
('Women''s Single Sculls','rowing','rowing','temps','FALSE'),
('Lightweight Men''s Double Sculls','rowing','rowing','temps','FALSE'),
('Lightweight Men''s Four','rowing','rowing','temps','FALSE'), 

('Women''s +75kg','weightlifting','weightlifting','score','FALSE'),
('Women''s 48kg','weightlifting','weightlifting','score','FALSE'),
('Women''s 53kg','weightlifting','weightlifting','score','FALSE'),
('Women''s 58kg','weightlifting','weightlifting','score','FALSE'),
('Women''s 63kg','weightlifting','weightlifting','score','FALSE'),
('Women''s 69kg','weightlifting','weightlifting','score','FALSE'),
('Women''s 75kg','weightlifting','weightlifting','score','FALSE'),
('Men''s +105kg','weightlifting','weightlifting','score','FALSE'),
('Men''s 105kg','weightlifting','weightlifting','score','FALSE'),
('Men''s 56kg','weightlifting','weightlifting','score','FALSE'),
('Men''s 62kg','weightlifting','weightlifting','score','FALSE'),
('Men''s 69kg','weightlifting','weightlifting','score','FALSE'),
('Men''s 77kg','weightlifting','weightlifting','score','FALSE'),
('Men''s 85kg','weightlifting','weightlifting','score','FALSE'),
('Men''s 94kg','weightlifting','weightlifting','score','FALSE'),

我正在尝试选择体育赛事较少的5个类别(水上,田径,射箭...)(男子100m,女子50m自由泳...)

在这种情况下,输出为:

Aquatics
Athletics
Shooting
badminton
rowing

谢谢

编辑1:添加了更多示例数据并希望输出。

2 个答案:

答案 0 :(得分:2)

请尝试一下。按语句分组,对类别进行分类,对每个类别按出现次数对订单结果进行排序,并将返回结果的数量限制为5

 select categories_sport, count(*) as cnt
   from Sports
  group by (categories_sport)
  order by cnt asc
  limit 5

答案 1 :(得分:0)

您想要一个聚合查询,其中包含GROUP BY categories_sport,按递增计数排序,并且对5个结果带有LIMIT子句。

这将在mysql和postgres(您都标记了两者)中都起作用:

SELECT categories_sport, count(*)
FROM sports
GROUP BY categories_sport
ORDER BY COUNT(*) 
LIMIT 5