首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

小菜一个select写法

2012-08-19 
小菜求助一个select写法有个表A,字段id,name。我要按name不同把所有记录和字段都select出来。这个select语句

小菜求助一个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)

热点排行