您的位置:程序门 -> ms-sql server -> 基础类



请帮忙看一下下列sql语句有什么错误谢谢


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请帮忙看一下下列sql语句有什么错误谢谢[已结贴,结贴人:hejiahuanle]
发表于:2007-06-28 14:11:19 楼主
a表中有如下几条记录
mc             bh
jc/m         1
kv/s         2
tm/ty       3
rt/b         4
ts/m         5

b表中字段
bh     name
  1       m
  3       ts

我想查询a表中的mc中   不包有   b表中name字段下的记录

select   *
from   a   cross   join
            b
where   (a.mc   not     like   '% '   +   name   +   '% ')    
这样不能查询出所想要的结果
发表于:2007-06-28 14:17:401楼 得分:2
select   a.*   from   a,b   where   a.bh   =   b.bh   and   charindex(b.name,a.mc)> 0
发表于:2007-06-28 14:17:422楼 得分:2
select   *   from   a   where   mc   not   in   (select   name   from   b)
发表于:2007-06-28 14:18:423楼 得分:6
drop   table   a,b
go
create   table   a(mc   varchar(10),bh   int)
insert   into   a
select   'jc/m ',1
union   all   select   'kv/s ',2
union   all   select   'tm/ty ',3
union   all   select   'rt/b ',4
union   all   select   'ts/m ',5
create   table   b(bh   int,name   varchar(10))
insert   into   b
select   1, 'm '
union   all   select   3, 'ts '

select   *   from   a
where   not   exists(select   1   from   b   where   charindex(b.name,a.mc)> 0)
/*
mc                   bh                    
----------   -----------  
kv/s               2
rt/b               4

(所影响的行数为   2   行)
*/
发表于:2007-06-28 14:19:174楼 得分:0

select   a.*   from   a
where   not   exists(select   name   from   b   where   '/ '   +   a.mc   +   '/ '   like   '%/ '   +   name   +   '/% ')
发表于:2007-06-28 14:19:365楼 得分:0
not   in   是否是完全相同的,我这是只要包含就不行
发表于:2007-06-28 14:20:026楼 得分:2
select   mc   ,bh
from   a  
where       left(mc,charindex( '/ ',mc)-1)     not   in   (select   name   from   b)
and     right(mc,charindex( '/ ',reverse(mc))-1)     not   in   (select   name   from   b)
发表于:2007-06-28 14:20:437楼 得分:0
或者

select   a.*   from   a
where   not   exists(select   name   from   b   where   charindex( '/ '   +   name   +   '/ ',   '/ '   +   a.mc   +   '/ ')   >   0)
发表于:2007-06-28 14:21:108楼 得分:8
create   table   a
(mc varchar(10),
  bh   int)
insert   a   select   'jc/m ',           1
union   all   select   'kv/s ',           2
union   all   select   'tm/ty ',       3
union   all   select   'rt/b ',             4
union   all   select   'ts/m ',           5

create   table   b
(bh int,
  name   varchar(10))
insert   b   select     1,       'm '
union   all   select     3,       'ts '
go
select   a.*   from   a
where   not   exists(select   name   from   b   where   '/ '   +   a.mc   +   '/ '   like   '%/ '   +   name   +   '/% ')

select   a.*   from   a
where   not   exists(select   name   from   b   where   charindex( '/ '   +   name   +   '/ ',   '/ '   +   a.mc   +   '/ ')   >   0)
go
drop   table   a,   b
/*
mc bh
kv/s 2
tm/ty 3
rt/b 4
*/
发表于:2007-06-28 14:22:029楼 得分:0
感覺按照樓主的需求,結果應該有3條紀錄。   '/ '應該是分隔符。
发表于:2007-06-28 14:26:1810楼 得分:0
谢谢你们


快速检索

最新资讯
热门点击