[ 本帖最后由 tomorrow819 于 2009-9-30 14:43 编辑 ]作者: lucash 时间: 2009-09-30 14:32
select * from test where id in (select id from (select class,min(id) id from test group by class)b);作者: tomorrow819 时间: 2009-09-30 14:55
谢谢,可是有语法错误作者: cenalulu 时间: 2009-09-30 14:59
select min(id),class,name from table_name group by class;作者: tomorrow819 时间: 2009-09-30 15:05
原帖由 cenalulu 于 2009-9-30 14:59 发表
select min(id),class,name from table_name group by class;
我有很多列,30多列吧,要取出所有的列,并且某些列名可能会变的,要用select *, 您给的例子是取出固定的列。作者: tomorrow819 时间: 2009-09-30 15:13
呵呵,综合了楼上两家的,用下面的sql搞定:
select * from table_name where id in (select min(id) from table_name group by class)作者: tomorrow819 时间: 2009-09-30 15:13
我不知道把分给谁好啊,好像不能一人给一点哦作者: cenalulu 时间: 2009-09-30 15:15
你的id是主键吧?
select * from table_name tn join (select min(id) as mid from table_name group by class) tn2 on tn.id=tn2.mid;作者: hb_li_520 时间: 2009-09-30 15:31
分应该给:
cenalulu 作者: lucash 时间: 2009-09-30 15:47
如何退分?作者: lucash 时间: 2009-09-30 15:48 标题: 回复 #3 tomorrow819 的帖子 我这里没有语法错误阿?你的什么版本?作者: lucash 时间: 2009-09-30 16:00
mysql> select * from test;
+------+-------+------+
| id | class | name |
+------+-------+------+
| 1 | AAA | ccc |
| 2 | BBB | mmm |
| 3 | AAA | kkk |
| 4 | AAA | iii |
+------+-------+------+
mysql> select * from test where id in (select id from (select class,min(id) id from test group by class)b);
+------+-------+------+
| id | class | name |
+------+-------+------+
| 1 | AAA | ccc |
| 2 | BBB | mmm |
+------+-------+------+