表a
custname 一 二
张三 张 三
李四 李 四
王五 王 五表b
username 一 二
张三 0 0
李家 0 0
赵六 0 0查询出的结果是
custname username 一 二 总数
张三 张三 1 1 2
张三 李家 0 0 0
张三 赵六 0 0 0
李四 张三 0 0 0
李四 李家 1 0 1
李四 赵六 0 0 0
王五 张三 0 0 0
王五 李家 0 0 0
王五 赵六 0 0 0小弟请各位大侠帮忙了!!
custname 一 二
张三 张 三
李四 李 四
王五 王 五表b
username 一 二
张三 0 0
李家 0 0
赵六 0 0查询出的结果是
custname username 一 二 总数
张三 张三 1 1 2
张三 李家 0 0 0
张三 赵六 0 0 0
李四 张三 0 0 0
李四 李家 1 0 1
李四 赵六 0 0 0
王五 张三 0 0 0
王五 李家 0 0 0
王五 赵六 0 0 0小弟请各位大侠帮忙了!!
,[一]=case when charindex(a.[一], username)>0 then 1 else 0 end
,[二]=case when charindex(a.[二], username)>0 then 1 else 0 end
,[总数]=case when charindex(a.[一], username)>0 then 1 else 0 end
+ case when charindex(a.[二], username)>0 then 1 else 0 end
from a,b
insert into a values('张三', '张' , '三')
insert into a values('李四', '李' , '四')
insert into a values('王五', '王' , '五')
create table b(username varchar(10), 一 varchar(10), 二 varchar(10))
insert into b values('张三' , 0 , 0)
insert into b values('李家' , 0 , 0)
insert into b values('赵六' , 0 , 0)
goselect a.custname , b.username,
(case when charindex(a.一,b.username) > 0 then 1 else 0 end) 一,
(case when charindex(a.二,b.username) > 0 then 1 else 0 end) 二,
(case when charindex(a.一,b.username) > 0 then 1 else 0 end) +
(case when charindex(a.二,b.username) > 0 then 1 else 0 end) 总数
from a,bdrop table a , b/*
custname username 一 二 总数
---------- ---------- ----------- ----------- -----------
张三 张三 1 1 2
李四 张三 0 0 0
王五 张三 0 0 0
张三 李家 0 0 0
李四 李家 1 0 1
王五 李家 0 0 0
张三 赵六 0 0 0
李四 赵六 0 0 0
王五 赵六 0 0 0(所影响的行数为 9 行)
*/
--> 测试数据:@a
declare @a table([custname] varchar(4),[一] varchar(2),[二] varchar(2))
insert @a
select '张三','张','三' union all
select '李四','李','四' union all
select '王五','王','五'
--> 测试数据:@b
declare @b table([username] varchar(4),[一] int,[二] int)
insert @b
select '张三',0,0 union all
select '李家',0,0 union all
select '赵六',0,0select custname, username
,[一]=case when charindex(a.[一], username)>0 then 1 else 0 end
,[二]=case when charindex(a.[二], username)>0 then 1 else 0 end
,[总数]=case when charindex(a.[一], username)>0 then 1 else 0 end
+ case when charindex(a.[二], username)>0 then 1 else 0 end
from @a a, @b b
/*
custname username 一 二 总数
-------- -------- ----------- ----------- -----------
张三 张三 1 1 2
李四 张三 0 0 0
王五 张三 0 0 0
张三 李家 0 0 0
李四 李家 1 0 1
王五 李家 0 0 0
张三 赵六 0 0 0
李四 赵六 0 0 0
王五 赵六 0 0 0(9 行受影响)
*/
declare @a table(custname varchar(10),[01] varchar(2),[02] varchar(2))
insert into @a select '张三','张','三'
insert into @a select '李四','李','四'
insert into @a select '王五','王','五' declare @b table(username varchar(10),[01] int,[02] int)
insert into @b select '张三',0,0
insert into @b select '李家',0,0
insert into @b select '赵六',0,0
select
a.custname,b.username,
[01]=(case when b.username like '%'+a.[01]+'%' then 1 else 0 end),
[02]=(case when b.username like '%'+a.[02]+'%' then 1 else 0 end),
[01]=(case when b.username like '%'+a.[01]+'%' then 1 else 0 end)+(case when b.username like '%'+a.[02]+'%' then 1 else 0 end)
from
@a a,@b b/*
custname username 01 02 01
---------- ---------- ----------- ----------- -----------
张三 张三 1 1 2
李四 张三 0 0 0
王五 张三 0 0 0
张三 李家 0 0 0
李四 李家 1 0 1
王五 李家 0 0 0
张三 赵六 0 0 0
李四 赵六 0 0 0
王五 赵六 0 0 0
*/
,[一]=case when charindex(a.[一], username)=1 then 1 else 0 end
,[二]=case when charindex(a.[二], username)=2 then 1 else 0 end
,[总数]=(case when charindex(a.[一], username)=1 then 1 else 0 end
+ case when charindex(a.[二], username)=2 then 1 else 0 end)
from a,b