CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

问题 关于sql查询

楼主bcy123456(bcy123456)2003-12-02 16:54:23 在 MS-SQL Server / 基础类 提问

我要查询数据库表里mm表,查询年龄大于50,我想在查询的结果里加一个序号,如1,2,3...,  
  如,select   name   from   mm   where   nl>50  
  有什么加一个序号,而且序号是顺序的  
  结果形式是:  
  1   bcy  
  2   ldy  
  3     ml  
  .       .  
  .       .  
  .       .  
  .       .  
  .       . 问题点数:0、回复次数:9Top

1 楼realgz(realgz)回复于 2003-12-02 16:58:56 得分 0

目前主要有2种方法  
  1,select   *   into   #tmp   from   table  
  alter   #tmp   add   cnt   identity(1,1)  
  select   *   from   #tmp  
  2,有一个可以唯一区分的列或列组合  
  select   *,(select   count(*)     from   table   t2   where   t2.id<=t.id)   as   cnt   from   table   tTop

2 楼wzh1215(懒猫)回复于 2003-12-02 16:59:10 得分 0

如果表里有主键:  
  select   (select   count(*)+1   from   mm   a   where   a.主键>主键   and   n1>50)   as   id,name   from   mm   where   n1>50Top

3 楼zarge(鲨去来兮)回复于 2003-12-02 16:59:11 得分 0

select   identity(int,   1,   1)   as   sn,   name   into   #tmp   from   mm   where   nl>50  
  select   *   from   #tmpTop

4 楼realgz(realgz)回复于 2003-12-02 16:59:39 得分 0

错了  
  1,select   *   into   #tmp   from   table  
  alter   #tmp   add   cnt   int     identity(1,1)  
  select   *   from   #tmp  
  2,要有一个可以唯一区分的列或列组合  
  select   *,(select   count(*)     from   table   t2   where   t2.id<=t.id)   as   cnt   from   table   t  
  Top

5 楼sdhdy(大江东去...)回复于 2003-12-02 17:00:04 得分 0

select   identity(int,1,1)   FID,name   into   #temp   from   from   mm   where   nl>50  
  select   *   from   #temp  
  drop   table   #tempTop

6 楼lansquenet(佣兵小队长)回复于 2003-12-02 17:00:32 得分 0

SELECT   identity(int,   1,   1)   id,   name   INTO   #   FROM   mm   WHERE   nl>50  
  SELECT   *   FROM   #  
  Top

7 楼sdhdy(大江东去...)回复于 2003-12-02 17:00:33 得分 0

select   identity(int,1,1)   FID,name   into   #temp     from   mm   where   nl>50  
  select   *   from   #temp  
  drop   table   #temp  
  Top

8 楼victorycyz(--)回复于 2003-12-02 17:03:21 得分 0

select   a.name   into   #A   from   MM   a   where   a.nl>50  
  alter   table   #A   add     id   int   identity(1,1)  
  select   *   from   #A  
  Top

9 楼xgli2002()回复于 2003-12-02 17:16:50 得分 0

create   table   #tem(no   int   identity,name   varchar(20))  
  insert   #tem   select   name   from     mm   where   n1>50  
  select   *   from   #tem  
  drop   table   #tem  
  Top

相关问题

  • sql查询
  • SQL SERVER 查询
  • sql查询??
  • sql 查询
  • sql查询
  • SQL查询
  • sql 查询
  • sql查询(select)
  • SQL查询
  • SQL查询

关键词

  • identity
  • 序号
  • nl
  • tmp
  • tem
  • mm
  • where
  • table
  • select
  • add

得分解答快速导航

  • 帖主:bcy123456

相关链接

  • SQL Server类图书

广告也精彩

反馈

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