我有一张桌子
city|locality
a | bc
a | dc
a | ef
a | gh
a | ij
我想创建一个组,这样它就可以显示
a |bc
|dc
|ef
|gh
|ij
我目前正在使用
select(*) from 'tablename' group by city;
但它只给我一个局部性的值。
发布于 2016-07-30 18:21:10
我不太确定这是否是您想要的,但是如果您想在一个新的单元格中获得a
的locality
中的所有行,您可以使用下面的代码。
SELECT *, GROUP_CONCAT(locality) as localities FROM table GROUP BY city
这将输出:
city localities
a bc, dc, ef, gh, ij
发布于 2016-07-30 18:33:28
解决方案是直接使用:
select city, locality from yourtable order by city, locality
然后,您可以通过仅在值发生更改时打印/使用city
来解决表示层中的问题。
类似于(在Java中):
String previousCity = null;
while (rs.next()) {
String currentCity = rs.getString("city");
if (Objects.equals(previousCity, currentCity) {
// Same as previous: no need to print so set blank
String currentCity = "";
} else {
previousCity = currentCity;
}
System.out.println(currentCity + " | " + rs.getString("locality"));
}
发布于 2016-07-30 18:25:16
试试这个,
select col1, GROUP_CONCAT(col2 SEPARATOR ';') from tablename;
它将为您提供如下结果:
A| bc;dc;ef;gh;ij
现在以你自己的方式使用它。
https://stackoverflow.com/questions/38672740
复制相似问题