表tbGg
xh country town     village
1  苏仙区  良田镇   良田村
2  苏仙区  良田镇   向阳村
3  苏仙区  邓家塘乡 邓家塘村
4  苏仙区  邓家塘乡 南水界村
1  桂阳县  桥头镇   桥头村
2  桂阳县  桥头镇   白溪村
3  桂阳县  城关镇   城头村
4  桂阳县  城关镇   二龙村
1  永兴县  城关镇   小林村
2  永兴县  城关镇   何家村
3  永兴县  玉泉乡  玉泉村
4  永兴县  玉泉乡  张家村
想对town排序
如果这样写会出错的?
select distinct town from tbjg where country='永兴县' order by xh 
这样写,查询结果不正确
select t.town from tbjg t where not exists (select 1 from tbjg where town = t.town and xh < t.xh) 
where country='永兴县' 
order t.xh 
应该怎样写呢?

解决方案 »

  1.   

    order by country, xh, town 看看是不是你要的结果
      

  2.   

    select distinct town from tbjg where country='永兴县' order by xh为什么加DISNTINCT? 去掉就行了
      

  3.   

    select  town from tbjg where country='永兴县'  GROUP BY town order by MIN(xh)
      

  4.   


    select distinct town from tbjg where country='永兴县' order by xh 
    这样写为什么会出错?没错误啊
      

  5.   

    order by 后面的字段 必须在前面select中有