选择表时,Sql删除重复

时间:2012-02-08 13:09:22

标签: c# .net sql

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

我想创建一个Select Command来获取所有城市,并从结果中删除重复项。

我正在使用的选择:

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

3 个答案:

答案 0 :(得分:4)

使用DISTINCT

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

答案 1 :(得分:0)

考虑以下一行:

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

答案 2 :(得分:0)

尝试此查询:

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

可以找到DISTINCT的文档here