Sql语句,分组问题

select tt from book group by tt

tt为字符型

这样分组后显示数据的顺序是按照tt在字母表中的顺序排列的~

我想让她按照ID的顺序排列,应该怎么办?

select tt from book group by tt order by id desc

select id,tt from book group by tt order by id desc
都是错误的!!!

请高手指点
---------------------------------------------------------------

select tt from (select tt,[id] from book order by [id]) as table
group by tt
---------------------------------------------------------------

应该这样:
select id,tt from book group by id,tt order by id desc

但其结果未必是你想要的 :(

---------------------------------------------------------------

楼上的好方法,学习
---------------------------------------------------------------

select max(id),tt from book group by tt order by id desc

---------------------------------------------------------------

select min(id),tt from book group by tt order by id desc
---------------------------------------------------------------

select * from (select id,tt from book group by id,tt) AA order by id desc

---------------------------------------------------------------

select min(id) as id, tt from book group by tt order by id
---------------------------------------------------------------

select tt from book group by tt order by min(id)
---------------------------------------------------------------

select min(id) as id, tt from book group by tt order by id

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus