我有一个表集团通过城市名,只有第一排
city|locality
a | bc
a | dc
a | ef
a | gh
a | ij
我想通过这样来创建一个组,它显示
a |bc
|dc
|ef
|gh
|ij
我在一次显示相同的城市名m目前正在使用
select(*) from 'tablename' group by city;
但它只给我一个地方值。
我有一个表集团通过城市名,只有第一排
city|locality
a | bc
a | dc
a | ef
a | gh
a | ij
我想通过这样来创建一个组,它显示
a |bc
|dc
|ef
|gh
|ij
我在一次显示相同的城市名m目前正在使用
select(*) from 'tablename' group by city;
但它只给我一个地方值。
不很确定,如果这是你想要的,但如果你想从locality
所有行的a
在一个新的单元格,可以用这个。
SELECT *, GROUP_CONCAT(locality) as localities FROM table GROUP BY city
这将输出:
city localities
a bc, dc, ef, gh, ij
iv treid this但它确实解决了问题..事情是我必须运行一个清理功能来逐行打印我的脚本以获得loacalities –
为什么不通过PHP在你的输出中解决这个问题? – Perry
是啊,我只是做了这让它更容易,我只是与价值.....分隔符......分隔符......它只是让我感到我的数组将看起来像在事件中,我设法得到我想要的东西 –
您可以通过编写2个查询尝试,
select distinct (city) from 'tablename';
然后U将得到城市为 'A';
select * from tablename where city='a';// 'a' means out put of first query
这真的是效率低下 –
试试这个,
select col1, GROUP_CONCAT(col2 SEPARATOR ';') from tablename;
它会给你喜欢的结果:
一个| bc; dc; ef; gh; ij
现在以您自己的方式使用它。
的解决方法就是使用:
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"));
}
不能由SQL – Jens
做真的什么左加入? –
@ReubenGomes你得到一个完整的行不是空白和值 – Jens