SQL 选择不同的组合。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6870397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting Distinct combinations.
提问by Codevalley
I have a table which has 2 fields (latitude, longitude) and many other fields. I want to select the distinct combinations of latitude and longitude from this table. what would be the query for that?
我有一个表,它有 2 个字段(纬度、经度)和许多其他字段。我想从此表中选择纬度和经度的不同组合。对此的查询是什么?
回答by SWeko
Simply use distinct
只需使用不同的
SELECT DISTINCT Latitude, Longitude
FROM Coordinates
This will return values where the (Latitude, Longitude)
combination is unique.
这将返回(Latitude, Longitude)
组合唯一的值。
This example supposes that you do not need the other columns. If you do need them, i.e. the table has Latitude, Longitude, LocationName
columns, you could either add LocationName
to the distinct list, or use something along the lines of:
此示例假设您不需要其他列。如果您确实需要它们,即表有Latitude, Longitude, LocationName
列,您可以添加LocationName
到不同的列表中,或者使用以下内容:
SELECT Latitude, Longitude, MIN(LocationName)
FROM Coordinates
GROUP BY Latitude, Longitude
回答by Dennis van Schaik
its an old post. but I just came across it while looking for an anser for the same problem. The above answer didn't work for me, but I found another simple solution using CONCAT():
它的旧帖子。但我只是在寻找同样问题的分析者时遇到了它。上面的答案对我不起作用,但我找到了另一个使用 CONCAT() 的简单解决方案:
SELECT *
FROM Coordinates
GROUP BY CONCAT(Latitude, Longitude);
This will give you all the unique Latitude / Longitude combinations, without any limitations to the select part of the query.
这将为您提供所有独特的纬度/经度组合,对查询的选择部分没有任何限制。
回答by Bax
I think it will be something about :
我认为这将是关于:
SELECT latitude, longitude
FROM table_name t1
INNER JOIN table_name t2
WHERE t1.latitude <> t2.latitude OR t1.longitude <> t2.longitude
That is the SELF INNER JOIN.
那就是 SELF INNER JOIN。
回答by Bala
SELECT COLUMN1,COLUMN2
FROM TABLE_NAME
GROUP BY COLUMN1,COLUMN2 -- This will fetch the unique combinations
-- During this kind of selection it is always better to give a where condition so that the results are filtered before grouping
Your Code:
您的代码:
SELECT Latitude, Longitude
FROM Coordinates
GROUP BY Latitude, Longitude