开发者

Nesting, grouping Sqlite syntax?

开发者 https://www.devze.com 2023-01-02 09:17 出处:网络
I can\'t for the life of me figure out this Sqlite syntax. Our database contains records like: TX, Austin

I can't for the life of me figure out this Sqlite syntax.

Our database contains records like:

TX, Austin
OH, Columbus
OH, Columbus
TX, Austin
OH, Cleveland
OH, Dayton
OH, Columbus
TX, Dallas
TX, Houston
TX, Austin
(State-field and a city-field.)

I need output li开发者_StackOverflow社区ke this:

OH: Columbus, Cleveland, Dayton
TX: Dallas, Houston, Austin
(Each state listed once... and all the cities in that state... [edited: each listed once])

What would the SELECT statement(s) look like?


You could use group_concat:

select state, group_concat(city)
from YourTable
group by state

In reply to your comment, you can use distinct:

select state, group_concat(distinct city)
                           ^^^^^^^^


A slight modification to Andomar's answer seems to do the trick, though I'm not sure that it's really intended to work:

select distinct state, group_concat(distinct city)
from YourTable
group by state
0

精彩评论

暂无评论...
验证码 换一张
取 消