当前位置: 代码迷 >> Sql Server >> 小菜一个select写法
  详细解决方案

小菜一个select写法

热度:42   发布时间:2016-04-27 12:10:55.0
小菜求助一个select写法
有个表A,字段id,name。
我要按name不同把所有记录和字段都select出来。这个select语句怎么写?
用select distinct name from A 只能显示name字段。用select distinct name,id from A 就不灵了。

------解决方案--------------------
select t.* from a t where id = (select min(id) from a where name = t.name)
select t.* from a t where not exists (select 1 from a where name = t.name and id < t.id)

如果上面不对,请:
SQL code
建议你提供详细的资料:例如表的结构,表之间的关系,测试数据,相关算法及需要的结果。这样有助于我们理解你的意思,更主要的是能尽快让你获得答案或解决问题的方法。
------解决方案--------------------
select id,name from tbl where id=(select min(id) from tbl b where tbl.name=b.name)
  相关解决方案