日期:2014-05-18  浏览次数:20546 次

达人帮忙看看这条SQL语句怎么写?
Table:class
 
id                     xueyuan
1                         信息
2                         信息
3                         化材
4                         化材
5                         经贸

现在我想通过SQL得到以下结果
 
xueyuan
信息
化材
经贸

就是把原来class表中的不重复的学院找出来,但是必须根据id号升序来

我自己原来写了这样:   select   distinct   xueyuan
                                          from   class
但是得出的结果是:
xueyuan
化材
经贸
信息

好像distinct会根据首个字母的顺序排的。
请教达人啊,谢谢!




------解决方案--------------------
select xueyuan,min(ID) id from class group by xueyuan order by ID
------解决方案--------------------
create table t(id int,xueyuan varchar(20))
insert t select 1, '信息 '
union all select 2, '信息 '
union all select 3, '化材 '
union all select 4, '化材 '
union all select 5, '经贸 '

select min(id) as id ,xueyuan from t group by xueyuan order by id



------解决方案--------------------
if(object_id( 'master..t ')is not null)
drop table t

create table t(id int,xueyuan varchar(20))
insert t select 1, '信息 '
union all select 2, '信息 '
union all select 3, '化材 '
union all select 4, '化材 '
union all select 5, '经贸 '


select xueyuan from t group by xueyuan order by min(id)

drop table t

结果:
信息
化材
经贸