CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

如何在一个表中搜索出相同字段的第一条记录

楼主xiaoduo(多多)2006-03-01 09:57:49 在 MS-SQL Server / 基础类 提问

表名是table1,其中有个字段account,我现在想做的就是如何把表中所有不同的account的第一条记录查询出来,这个语句该怎么写? 问题点数:20、回复次数:9Top

1 楼flyeq008()回复于 2006-03-01 10:26:45 得分 0

select   top   1   account   from   table1  
  group   by   account  
  having   count(account)>1Top

2 楼xiaoduo(多多)回复于 2006-03-01 10:32:04 得分 0

这个table1中,我需要查询出来的结果除了account这个字段外,还需要包括其它的字段比如timeTop

3 楼wood111(木头一根)回复于 2006-03-01 10:52:24 得分 0

select   *   from   table1   where   account   in   (  
  select   top   1   account   from   table1  
  group   by   account  
  having   count(account)>1  
  )Top

4 楼msjqd(黑色幽默)回复于 2006-03-01 12:52:30 得分 0

select   A.*    
      from   test   A  
    where   name   =   (  
                      select   top   1   name   from   test   where   dis   =   A.dis  
                )Top

5 楼msjqd(黑色幽默)回复于 2006-03-01 12:54:02 得分 0

create   table   test  
  (  
      dis   varchar(20),  
      name     varchar(200),  
      count   int  
  )  
   
  insert   into   test   select   '110000',                     '北京市',     10  
  insert   into   test   select   '110000',                     '北京市朝阳区',   20  
  insert   into   test   select   '110000',                     '北京市海啶区',20  
  insert   into   test   select   '120000',                     '天津市'           ,8  
  insert   into   test   select   '120000',                     '天津市**区'   ,6    
  insert   into   test   select   '120031',                     '天津市**区'   ,   10  
  insert   into   test   select   '420000',                   '湖北省'     ,3000  
  insert   into   test   select   '420000',                   '湖北省武汉市',   2000  
  insert   into   test   select   '420200',                   '湖北省武汉市江岸区',   300  
  insert   into   test   select   '420200',                   '湖北省武汉市武昌区',   200  
   
   
    select   dis  
        from   test  
      group   by   dis  
    having   count(dis)   >   1  
   
  select   A.*    
      from   test   A  
    where   name   =   (  
                      select   top   1   name   from   test   where   dis   =   A.dis  
                )  
     
   
  drop   table   test  
   
   
  dis             name           count  
  110000 北京市 10  
  120000 天津市 8  
  120031 天津市**区 10  
  420000 湖北省 3000  
  420200 湖北省武汉市江岸区 300  
  Top

6 楼msjqd(黑色幽默)回复于 2006-03-01 12:55:02 得分 0

sorry      
   
        select   dis  
        from   test  
      group   by   dis  
    having   count(dis)   >   1  
   
    是错误的!测试时没有删除Top

7 楼sxlcom(木头)回复于 2006-03-01 13:05:01 得分 0

select    
          t.*    
  from    
          表1     t    
  where    
          t.ss   in(select   ss   from   表1   group   by   ss   having   count(*)   >   1)Top

8 楼mislrb(上班看看早报,上上CSDN,下班看看电影)回复于 2006-03-01 13:14:48 得分 0

--如果有ID字段的话(id   int   identity(1,1)  
   
  select   *   from   yourtable   t  
  where   not   exists(select   1   from   yourtable   where   id<t.id   and   account=t.account)  
   
  --如果没有  
  select   id=identity(int,1,1),*   into   #t   from   yourtable  
  select   *   from   #t   t  
  where   not   exists(select   1   from   #t   where   id<t.id   and   account=t.account)  
  Top

9 楼lgjlry(455)回复于 2006-03-01 17:33:27 得分 0

学习Top

相关问题

  • 如何搜索表中某字段的值相同所有记录
  • 搜索出不重复的记录,按某一字段排序
  • 如何搜索某个字符串在N个表的那个字段中,并一次性修改N个表中这个搜索到的字段的所有记录
  • aw字段内加空记录使记录数和B表的bw字段中记录数一样多?
  • 如何有效率的搜索第一个表里的ID字段在第二个表里没有的全部记录?
  • Sql怎样搜索一个字段为空且其它字段满足要求的记录?
  • 如何搜索数据库字段里面只有一个字符的记录?
  • 请问将一个表记录转成表字段的算法?
  • 怎么用VB获得数据库表的字段个数和各字段的字段名(包括无记录的表和有记录的表)?
  • 如何查询表中某字段为空的记录?

关键词

  • 字段
  • top
  • 表
  • account
  • 湖北省
  • 天津市
  • insertinto test
  • dis
  • ss
  • 北京市

得分解答快速导航

  • 帖主:xiaoduo

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo