--数据测试环境
declare @t1 table(id int,afields varchar(30))
insert into @t1
select 1,'A+B+C'
union all select 2,'B+C'
union all select 3,'A+C'
union all select 4,'C'
union all select 5,'B+A'
declare @t2 table(id int,bfields varchar(30))
insert into @t2
select 1,'A+D+C'
union all select 2,'A+W'
union all select 3,'D+C'
union all select 4,'D+F+C'
union all select 5,'C+D'
--查询处理
select a.*
from @t2 a join(
select distinct bb.id from(
select a.id,afields=substring(afields,b.id,charindex('+',afields+'+',b.id)-b.id)
from @t1 a,(
select id=a.id+b.id from(
select id=0 union all select 1 union all select 2 union all select 3 union all select 4
union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
)a,(
select id=0 union all select 10 union all select 20 union all select 30 union all select 40
union all select 50 union all select 60 union all select 70 union all select 80 union all select 90) b
) b where substring('+'+afields,b.id,1)='+'
)aa full join( --*************** aa ***********************
select a.id,bfields=substring(bfields,b.id,charindex('+',bfields+'+',b.id)-b.id)
from @t2 a ,(
select id=a.id+b.id from(
select id=0 union all select 1 union all select 2 union all select 3 union all select 4
union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
)a,(
select id=0 union all select 10 union all select 20 union all select 30 union all select 40
union all select 50 union all select 60 union all select 70 union all select 80 union all select 90) b
) b where substring('+'+bfields,b.id,1)='+'
)bb on aa.id=bb.id and aa.afields=bb.bfields --********** bb *********
where aa.id is not null
) b on a.id=b.id
/*--测试结果
id bfields
----------- ------------------------------
1 A+D+C
3 D+C
4 D+F+C
(所影响的行数为 3 行)
--*/
---------------------------------------------------------------
补充1: 以上是对的,但若将full join 改为 join 就会出错.
---------------------------------------------------------------
补充2: 若将 aa,bb的内容分别先存到临时表,再用临时表来join也是可以的
补充3:
最后几行
on aa.id=bb.id and aa.afields=bb.bfields --********** bb *********
where aa.id is not null
) b on a.id=b.id
若改成
on aa.id=bb.id --and aa.afields=bb.bfields --********** bb *********
where aa.id is not null
) b on a.id=b.id
也是可以的,但结果就不对了。
---------------------------------------------------------------
估计是表变量的缺陷吧!
---------------------------------------------------------------
与表变量应该无关,我试过,改表实际的表也是一样的。
---------------------------------------------------------------
这么长?那么多substring,头晕。
大概看了一下,好像是因为join 消除了哪笔记录。。。。
---------------------------------------------------------------
哦,长是长点,但结构清晰,且能COPY到查询分析器中直接运行。
---------------------------------------------------------------
什么目的呀!这么长!
---------------------------------------------------------------
又看了一下,实在是很强,比刚看的时候晕 :)
---------------------------------------------------------------
先收藏
---------------------------------------------------------------
不对,根本没错。
我把 aa、bb视图内容 Insert 到一张临时表里就什么都好使了!!
---------------------------------------------------------------
楼上,这种做法早就试过了,所以更奇怪了。
---------------------------------------------------------------
--帮你断下括号:不然没对齐每办法快速定位:
declare @t1 table(id int,afields varchar(30))
insert into @t1
select 1,'A+B+C'
union all select 2,'B+C'
union all select 3,'A+C'
union all select 4,'C'
union all select 5,'B+A'
declare @t2 table(id int,bfields varchar(30))
insert into @t2
select 1,'A+D+C'
union all select 2,'A+W'
union all select 3,'D+C'
union all select 4,'D+F+C'
union all select 5,'C+D'
--查询处理
select a.*
from @t2 a
join
(
select distinct bb.id from
(
select a.id,afields=substring(afields,b.id,charindex('+',afields+'+',b.id)-b.id)
from @t1 a, (
select id=a.id+b.id from
(
select id=0 union all select 1 union all select 2 union all select 3 union all select 4
union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
)a,
(
select id=0 union all select 10 union all select 20 union all select 30 union all select 40
union all select 50 union all select 60 union all select 70 union all select 80 union all select 90
) b
) b where substring('+'+afields,b.id,1)='+'
) aa
full join
( --*************** aa ***********************
select a.id,bfields=substring(bfields,b.id,charindex('+',bfields+'+',b.id)-b.id)
from @t2 a ,
(
select id=a.id+b.id from
(
select id=0 union all select 1 union all select 2 union all select 3 union all select 4
union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
)a,
(
select id=0 union all select 10 union all select 20 union all select 30 union all select 40
union all select 50 union all select 60 union all select 70 union all select 80 union all select 90
) b
) b where substring('+'+bfields,b.id,1)='+'
)bb on aa.id=bb.id and aa.afields=bb.bfields --********** bb *********
where aa.id is not null
) b on a.id=b.id
/*--测试结果
id bfields
----------- ------------------------------
1 A+D+C
3 D+C
4 D+F+C
(所影响的行数为 3 行)
--*/
---------------------------------------------------------------
好玩,我妄图用left join 和right join 找出问题出在哪边,但但但。。。。。。。。
真好玩。。
---------------------------------------------------------------
好像是Cross join 导致的问题。。。。他在分析的时候可能以为有外连接,然后不知道怎么执行执行出了错误结果。。。。。指定了外连接以后分析的话直接用外连接分析,它就不出错了!!!
我 把
(
select id=a.id+b.id from
(
select id=0 union all select 1 union all select 2 union all select 3 union all select 4
union all select 5 union all select 6 union all select 7 union all select 8 union all select 9
)a,
(
select id=0 union all select 10 union all select 20 union all select 30 union all select 40
union all select 50 union all select 60 union all select 70 union all select 80 union all select 90
) b
)
这个视图替换以后就好使了。
中间我 尝试使用不同的outer join 来确定问题所在,发现排除了
aa.afields =bb.bfields 条件进行join 时写法是标准写法时无差别均可得出结果,使用 = 或= 时会提示“在包含已联接的表的查询中,不能指定外联接操作符。”SQL Servr 疯了。。。。
---------------------------------------------------------------
我也有个很怪的问题
执行下面的语句
--------------------------------------------------
select 厂内料号
from
(
select 厂内料号,convert(int,substring(厂内料号,charindex('-',厂内料号,1)+1,len(厂内料号)-8-charindex('-',厂内料号,1))) as 层数,floor(round(1/单PCS重量,0)) as 块数 --注意***** floor(round(1/单PCS重量,0))
from tblproduct_num where 单PCS重量 is not null and 单PCS重量<>0
) as a
where a.层数='4' and a.块数=6
---------------------------------------------
报错
Divide by zero error encountered.
但我执行
select 厂内料号,convert(int,substring(厂内料号,charindex('-',厂内料号,1)+1,len(厂内料号)-8-charindex('-',厂内料号,1))) as 层数,floor(round(1/单PCS重量,0)) as 块数 from tblproduct_num where 单PCS重量 is not null and 单PCS重量<>0
又没问题
郁闷了半天
不知道sql server中嵌套语句是如何判断where 条件的
---------------------------------------------------------------
昏,
不过有一点:加不加order by 的结果不一样是肯定的。
测试:
create table t1(a int)
in