我无法终生弄清楚这种Sqlite语法。

我们的数据库包含如下记录:


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.)



我需要这样的输出:


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



SELECT语句是什么样的?

最佳答案

您可以使用group_concat

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


要回复您的评论,您可以使用distinct

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

关于sql - 嵌套,分组Sqlite语法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2969103/

10-11 05:51