sql - 嵌套,分组Sqlite语法?

标签 sql sqlite select syntax

我无法终生弄清楚这种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/

相关文章:

sql - 在 Presto 查询中加密/混淆结果的想法?

iphone - 将图像存储到sqlite数据库中

连接 CTE 时 SQL Server 语法错误

javascript - 单击选择标签时,为什么 IE 不取消选择输入中的文本

java - 从多个表中检索数据 - Android Eclipse

SQL 根据条件获取行

sql - 不包括加入的权利的代码解释?

PHP:数据库缓存(内存中有 sqlite)

带有来自另一个表的列值的 MYSQL IN 语句

sqlite - SQLite喜欢vs Substr