如何在数据库中对数据进行分组 [英] How Do I group data in a db

查看:300
本文介绍了如何在数据库中对数据进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个收集各个国家和城市的数据库。



我我想要按国家/地区导出数据。那就是我需要一个查询来选择不同的国家,然后列出每个国家的所有城市。



我现在的查询是:SELECT distinct city来自`data_table` GROUP BY位置



虽然不行。



请帮助。谢谢

解决方案

不,这不起作用,因为City不在GROUP BY子句中,GROUP BY返回各行 - 所以你不能选择其中有多行!

请尝试:

  SELECT  city < span class =code-keyword> FROM  data_table  GROUP   BY  location,city 


Quote:

我现在的查询是:SELECT distinct city FROM` data_table` GROUP BY location







1)你还没有提到 Group by 您选择列表中的列

2)您不需要Group by Clause,因为您没有Any Aggregate函数在您的选择列表中



更改为



 选择  distinct  city  FROM `data_table`  GROUP  < span class =code-keyword> BY  location,city 



SELECT distinct city FROM `data_table`


Hello all,

I have a db that collects various countries and cities.

I'll like to export the data by Countries. That is I need a query that will select the distinct countries, and then also list all the cities of each country.

The query I have now is: SELECT distinct city FROM `data_table` GROUP BY location

Its not working though.

Kindly help. Thank you

解决方案

No, that won't work, because City is not in the GROUP BY clause, and GROUP BY returns individual rows - so you can't select multiple rows out of that!
Instead try:

SELECT city FROM data_table GROUP BY location, city


Quote:

The query I have now is: SELECT distinct city FROM `data_table` GROUP BY location




1) you haven't mentioned your Group by column in Your Select List
2)you don't need Group by Clause because you didn't have Any Aggregate function in your Select List

Change into

SELECT distinct city FROM `data_table` GROUP BY location,city

or 

SELECT distinct city FROM `data_table`


这篇关于如何在数据库中对数据进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆