得到了这个SQL:
SELECT DISTINCT(LocCity), LocZipCode FROM exp_course_events order by LocCity
和这个数据:
INSERT INTO `exp_course_events` (`LocCity`, `LocZipCode`) VALUES
('Aguadilla', '00602'), ('Akron', '44300'),('Akron', '44333'),
('Albany', '12205'), ('Albuquerque', '87102'),
('Albuquerque', '87109'), ('Austin', '78741'),
('Austin', '78753'), ('Austin', '78757'),
('Bend', '97701'), ('San Antonio', '78200'),
('San Antonio', '78201'),
....
('San Antonio', '78207');
每个LocCity只需为LocZipCode返回一个值,优先选择最小数量的LocZipCode 对于那个LocCity。
这是我想要的结果:
Aguadilla, 00602
Akron, 44300
Albany, 12205
Albuquerque, 87102
Austin, 78741
Bend, 97701
San Antonio, 78200
San Diego, 92108
San Francisco, 94111
San Juan, 00926
Santa Clara (San Jose), 95054
Springdale, 72762
Springfield, 62703
St. Louis. 63105
Visalia, 993291
Waco, 76705
Warwick, 02886
Waukesha, 53186
West Chester, 45069
West Des Moines, 50300
答案 0 :(得分:3)
SELECT LocCity, MIN(LocZipCode)
FROM exp_course_events
GROUP BY LocCity
ORDER BY LocCity
在此处阅读GROUP BY条款。
答案 1 :(得分:1)
select LocCity, min(LocZipCode)
from exp_course_events
group by LocCity
答案 2 :(得分:1)
具有GROUP BY
聚合的简单MIN()
即可完成。请务必使用名称(例如MIN()
)为AS LocZipCode
列添加别名,以便您可以更轻松地在应用程序端获取它。
SELECT LocCity, MIN(LocZipCode) AS LocZipCode FROM exp_course_events GROUP BY LocCity;
答案 3 :(得分:1)
SELECT LocCity
, MIN(LocZipCode)
FROM exp_course_events
GROUP by LocCity
order by LocCity