简体   繁体   English

如何在结果上选择ID区分?

[英]How to select distinct with ID on the result?

How to select distinct from table including ID column on the result? 如何在结果中选择与包括ID列的表不同?

Like for example: (this is error query) 例如:(这是错误查询)

SELECT ID,City,Street from (SELECT distinct City, Street from Location)

The table Location 桌子位置

CREATE TABLE Location(
ID int identity not null,
City varchar(max) not null,
Street varchar(max) not null
)

Then it will show the column ID, distinct column City, distinct column Street 然后它将显示列ID,不同的列City,不同的列Street

Is there a possible query to have this result? 是否有可能产生此结果的查询?

If you want for instance the lowest id for the unique data you desire you can do 例如,如果您想要唯一数据的最低ID,则可以执行此操作

select min(id), City, Street 
from Location
group by City, Street

Generally you have to tell the DB what id to take using an aggregate function like min() or max() 通常,您必须使用min()max()类的聚合函数来告诉数据库采用什么ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM