如何在数据库中对数据进行分组 [英] How Do I group data in a db
问题描述
大家好,
我有一个收集各个国家和城市的数据库。
我我想要按国家/地区导出数据。那就是我需要一个查询来选择不同的国家,然后列出每个国家的所有城市。
我现在的查询是: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,因为您没有AnyAggregate函数
在您的选择列表中
更改为
选择跨度> 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 yourGroup by
column in Your Select List
2)you don't need Group by Clause because you didn't have AnyAggregate 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屋!