简体   繁体   English

Mysql Group相同的值一起显示所有行

[英]Mysql Group same value together and show all rows

I have a table have city column 我有一张桌子有城市专栏

I want to group all same city together (GROUP BY will only show 1 rows, but I want all rows) 我想将所有相同的城市归为一组(GROUP BY仅显示1行,但我希望所有行)

ex. 例如

LA
New York
LA
LA
New York

output
New York
New York
LA
LA
LA

您正在寻找ORDER BY而不是GROUP BY

You need order by instead of group by 您需要排序依据而不是分组依据

select * from table order by city

There might be another issue when you have city names mixed with lower and upper case and it will not order properly in that case so you need to format the order by as 当城市名称混合使用大写和小写字母时,可能会出现另一个问题,因此在这种情况下排序不正确,因此您需要使用

select upper(city) from test
order by lower(city);

or 要么

select city from test
order by lower(city);

order by will helps you order by将帮助您

select * from tablename order by city

Try with length of the string 尝试使用字符串的长度

select * from cityname order by CHAR_LENGTH(city) desc

Working Fiddle 工作小提琴

Try ORDER BY as other answers suggest (the one from Abhik seems very complete) 尝试其他答案提示的ORDER BY (Abhik的答案似乎很完整)

Aside from that If you want to know how many cities of each type do you have you can do something like this: 除此之外,如果您想知道每种类型有多少座城市,可以执行以下操作:

SELECT city, COUNT(city) FROM table GROUP BY city ORDER BY city

This query will return 该查询将返回

LA | 洛杉矶| 3 3

New York | 纽约| 2 2

This will give you a better way to count your cities (if you want to do something like this) 这将为您提供更好的城市计数方法(如果您想做这样的事情)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM