Sql在选择表时删除重复项

本文关键字:删除 选择 Sql | 更新日期: 2023-09-27 18:23:41

我的sql中有一个表,例如有名称和城市。

我想制作一个选择命令,获取所有城市并从结果中删除重复的城市。

对于我正在使用的选择:

        string citiesSelect = "SELECT [city] FROM [people] ORDER BY [city]";
        CitiesDataSource.SelectCommand = citiesSelect;
        CitiesDataSource.Select(DataSourceSelectArguments.Empty);
        DropDownList2.DataSourceID = CitiesDataSource.ID;

Sql在选择表时删除重复项

使用DISTINCT。

 string citiesSelect = "SELECT DISTINCT [city] FROM [people] ORDER BY [city]";
        CitiesDataSource.SelectCommand = citiesSelect;
        CitiesDataSource.Select(DataSourceSelectArguments.Empty);
        DropDownList2.DataSourceID = CitiesDataSource.ID;

考虑以下行:

string citiesSelect = "SELECT DISTINCT [city] FROM [people] ORDER BY [city]";

尝试此查询:

SELECT DISTINCT [city] FROM [people] ORDER BY [city]

DISTINCT的文档可以在这里找到。