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

这样的SQL语句怎么写?答对者有分

楼主lbkbox(遗忘的诺言)2006-07-01 10:37:37 在 MS-SQL Server / 应用实例 提问

我有一个表:如下  
      表名:TABLE1  
      三个字段分别是:编码,名称,阶数  
    表中的数据如下:  
  ................................................  
          编码                 名称                               阶数  
          001                   华南地区                         1  
          001001             广东                                 2  
          002                   西南地区                         1  
          002001             广西                                 2  
          003                   华东地区                         1    
          003001             浙江省                             2  
          003001001       杭州市                             3  
  ................................................  
   
  怎样用一和SQL语句找出没有下级的记录数据 问题点数:100、回复次数:14Top

1 楼seafer(大旗)回复于 2006-07-01 10:46:19 得分 0

select   *   from   table1   a  
    where   not   exists(select   1   from   table1   b  
                                        where   b.no   like   a.no   ||   '%'   and   b.no   <>   a.no);Top

2 楼seafer(大旗)回复于 2006-07-01 10:47:23 得分 10

sorry,     写成oracle的语句了,  
  select   *   from   table1   a  
    where   not   exists(select   1   from   table1   b  
                                        where   b.no   like   a.no   +   '%'   and   b.no   <>   a.no)  
  Top

3 楼wwh999(印钞机V2.0...开发中....)回复于 2006-07-01 11:43:25 得分 10

select    
          *    
  from    
          Table1   A    
  where    
          not   exists(select   阶数=A.阶数+1   from   Table1)  
   
  ---------------------------------------------------------  
  你的分级条件是不是以前缀编码来区分的?Top

4 楼LouisXIV(夜游神)回复于 2006-07-01 11:47:10 得分 0

貌似是靠编码来区分的Top

5 楼wwh999(印钞机V2.0...开发中....)回复于 2006-07-01 11:49:11 得分 10

上面那句是用在分级层次比较固定的情况之下  
  下面的,是按照编码的情况来判定的.(如果编码是按级递增位数的话,可以使用)  
  ------------------------------------------------------------------------  
  select    
          *    
  from    
          Table1   A    
  where    
          not   exists(select   1   from   Table1   where   编码   like   A.编码+   '_%')Top

6 楼wwh999(印钞机V2.0...开发中....)回复于 2006-07-01 11:50:15 得分 10

似乎连在一起写,短一些...嘿嘿!  
  --------------------------------------------------------------------------------  
  select   *   from   Table1   A   where   not   exists(select   1   from   Table1   where   编码   like   A.编码+   '_%')Top

7 楼wwh999(印钞机V2.0...开发中....)回复于 2006-07-01 11:53:15 得分 10

答贴答迷糊了,3楼那贴是错的,LZ不要看了,改为  
  -------------------------------------------------------  
  select   *   from   Table1   A   where   not   exists(select   1   from   Table1   where   阶数=A.阶数+1   )  
  Top

8 楼fcuandy(了此残生.)回复于 2006-07-01 12:13:32 得分 10

SELECT   *   FROM   Table1   a   WHERE   1>(SELECT   COUNT(1)   FROM   Table1   b   WHERE   CHARINDEX(','   +   a.编码,','+b.编码)>0   AND   a.编码<>b.编码)Top

9 楼fcuandy(了此残生.)回复于 2006-07-01 12:19:55 得分 10

或者  
  SELECT   *   FROM   Table1   a   WHERE   1>(SELECT   COUNT(1)   FROM   Table1   b   WHERE   b.编码   LIKE   a.编码   +   '[0-9]%')  
  Top

10 楼ls0611(allan)回复于 2006-07-01 12:50:02 得分 10

select   *   from   Table1   A    
  where   not   exists(select   1   from   Table1   where   编码   like   A.编码+   '_%'   and   阶数=A.阶数+1)  
  Top

11 楼ls0611(allan)回复于 2006-07-01 13:08:17 得分 10

declare   @tb   table(  
  编码   char(10),    
  名称   char(10),  
  阶数   int)  
  insert   into   @tb  
  select'001','华南地区',1   union   all  
  select'001001','广东',2   union   all  
  select'002','西南地区',1   union   all  
  select'002001','广西',2   union   all  
  select'003','华东地区',1   union   all  
  select'003001','浙江省',2   union   all  
  select'003001001','杭州市',3  
   
  select   *   from   @tb   A    
  where   not   exists(select   *   from   @tb   where     编码   like   rtrim(A.编码)+   '_%'   and   阶数=A.阶数+1)Top

12 楼wwh999(印钞机V2.0...开发中....)回复于 2006-07-01 13:37:26 得分 8

哇,跟贴的这么多...虽然没有什么新意,但是热情可嘉....呵呵!Top

13 楼lbkbox(遗忘的诺言)回复于 2006-07-03 11:31:53 得分 0

我先验证一下,看看谁的是正确的,引出这么我高手,可喜可贺。Top

14 楼bugchen888(臭虫)回复于 2006-07-03 15:14:12 得分 2

select   *   from   TABLE1   a  
  where   not   exists  
  (  
  select   1  
  from   TABLE1   b  
  where   len(b.编码)>len(a.编码)  
  and   a.编码=left(b.编码,len(a.编码))  
  )Top

相关问题

关键词

得分解答快速导航

  • 帖主:lbkbox
  • seafer
  • wwh999
  • wwh999
  • wwh999
  • wwh999
  • fcuandy
  • fcuandy
  • ls0611
  • ls0611
  • wwh999
  • bugchen888

相关链接

  • SQL Server类图书

广告也精彩

反馈

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