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

求教一查询语句!!!救急啊!!高手们!!
表A       name       scot
              a             1
              b             1
              c             1
              d             2
              e             2

实现成:     a         1
                    d         2

问题2:   实现成:     a   b   1
                                    a   c   1
                                    b   c   1
                                    d   e   2

------解决方案--------------------
1. select min(name),scot from A group by scot
------解决方案--------------------
select
t.*
from
表 t
where
not exists(select 1 from 表 where name <t.name and scot=t.scot)

select
a.name,b.name,a.scot
from
表 a,表 b
where
a.scot=b.scot and a.name <b.name
order by
a.scot.a.name,b.name
------解决方案--------------------
表A name scot
a 1
b 1
c 1
d 2
e 2

实现成: a 1
d 2

select min(name) name,scot from tb group by scot
------解决方案--------------------
--------------例子--------
create table A(name varchar(10),scot int)
insert A
select 'a ',1 union all
select 'b ',1 union all
select 'c ',1 union all
select 'd ',2 union all
select 'e ',2

select min(name),scot from a group by scot

select
a.name,b.name,a.scot
from
a,a b
where
a.scot=b.scot and a.name <b.name
order by
a.scot,a.name,b.name