27,579
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据: [表A]
if object_id('[表A]') is not null drop table [表A]
create table [表A] (ck_imei int,ck_model varchar(5))
insert into [表A]
select 5323,'GQ728'
--> 测试数据: [表B]
if object_id('[表B]') is not null drop table [表B]
create table [表B] (inbox_mobile bigint,inbox_msg varchar(100))
insert into [表B]
select 15842569863,'a,0,333,22,0,V53B73,5323'
union
select 15842569863,'a,0,333,5323,0,V53B73,532' --中间有,最后不是
select t2.* from [表A] t1,[表B] t2
where t1.ck_imei=reverse(left(reverse(t2.inbox_msg),CHARINDEX(',',reverse(t2.inbox_msg))-1)) --保证最后一个,而中间有的不会出现
and t1.ck_model='GQ728'
/*
inbox_mobile inbox_msg
15842569863 a,0,333,22,0,V53B73,5323
*/
select * from B where exists(select 1 from A where right(b.inbox_msg,len(A.ck_imei))=A.ck_imei)
select b.*
from a,b
where charindex(','+ltrim(a.ck_imei)+',',','+b.inbox_msg+',')>0
and a.ck_model='GQ728'
select b.*
from a,b
where chaindex(','+ltrim(a.ck_imei)+',',','+b.inbox_msg+',')>0
and a.ck_model='GQ728'
set nocount on
go
if object_id('testa') is not null drop table testa
create table testa (ck_imei int,ck_model varchar(5))
insert into testa
select 5323,'GQ728'
--> 测试数据: testb
if object_id('testb') is not null drop table testb
create table testb (inbox_mobile bigint,inbox_msg varchar(100))
insert into testb
select 15842569863,'a,0,333,22,0,V53B73,5323'
union
select 15842569863,'a,0,333,5323,0,V53B73,532'
go
select testb.*
from testa inner join testb on ','+ltrim(testa.ck_imei)=right(testb.inbox_msg,len(testa.ck_imei)+1)
-------------
/*
inbox_mobile inbox_msg
-------------------- ----------------------------------------------------------------------------------------------------
15842569863 a,0,333,22,0,V53B73,5323
*/