Select * From tablename where class_id in (select distinct class_id from tablename)
select Class_ID,otherfields from Table group by Class_id
to blackfiles(从来是我伤心): 你取的是class_id记录数不止一条的classid to N_chow(一劍飄香): 你的结果跟没条件是一样的! to nashan(浪客剑心): 你的语法不正确,group by 分组函数必须有聚集函数! 这条语句看具体的需求,如果对于其他字段没什么要求,可以在nashan(浪客剑心)的语句上地otherfields上加上聚集函数,如max,min,sum()等,即可
明白你的意思。sql语句固然强大,但这个问题不是一句sql就能解决的。
这是个最普通的sql需求 select * from table where id in (select min(id) from table group by Class_id) id为table的PK
select * from TableName a, ( select Class_ID,min(Key) as Key from TableName group by Class_id ) b where a.key=b.key其中Key为你的表的主键或唯一索引.
对了,你的通过其他的主键在用class_id分组的情况下取一条最大或小的纪录。
同意supine 如果除了class_id外还有a,b,c等字段,那么可以用以下语句 select class_id,min(a) as a,min(b) as b,min(c) as c from tablename group by class_id
你取的是class_id记录数不止一条的classid
to N_chow(一劍飄香):
你的结果跟没条件是一样的!
to nashan(浪客剑心):
你的语法不正确,group by 分组函数必须有聚集函数!
这条语句看具体的需求,如果对于其他字段没什么要求,可以在nashan(浪客剑心)的语句上地otherfields上加上聚集函数,如max,min,sum()等,即可
select * from table where id in (select min(id) from table group by Class_id)
id为table的PK
(
select Class_ID,min(Key) as Key
from TableName
group by Class_id
) b
where a.key=b.key其中Key为你的表的主键或唯一索引.
如果除了class_id外还有a,b,c等字段,那么可以用以下语句
select class_id,min(a) as a,min(b) as b,min(c) as c from tablename group by class_id