两表关联查询如何使用like???

qingniao8 2011-01-14 10:52:01
表T1
NO
---------
LSV123
LBV124
LBV126
LCV225
DVB630

表T2
NO
----------
123
126
225
请问如何得到这样的结果:
NO
------
LSV123
LBV126
LCV225

谢谢!!!
...全文
361 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
joyhen 2011-01-14
  • 打赏
  • 举报
回复
--1
create table t1
(
no varchar(10)
)
insert into t1
select 'LSV123' union all
select 'LBV124' union all
select 'LBV126' union all
select 'LCV225' union all
select 'DVB630'
go
create table t2
(
no varchar(10)
)
insert into T2
select '123' union all
select '126' union all
select '225'
go

--2
;with newtb as(
select no, right(no,3) as co from t1
)
select newtb.no from newtb left join t2 on t2.no=newtb.co where t2.no is not null

--no
------------
--LSV123
--LBV126
--LCV225
--
--(3 行受影响)

--3
drop table t1,t2
qingniao8 2011-01-14
  • 打赏
  • 举报
回复
感谢感谢!
AcHerat 元老 2011-01-14
  • 打赏
  • 举报
回复

create table t1(id int,nob varchar(10))
insert into t1
select 1,'LSV123' union all
select 2,'LBV124' union all
select 3,'LBV126' union all
select 4,'LCV225' union all
select 5,'DVB630'
go

create table t2(noe varchar(10))
insert into t2
select '123' union all
select '126' union all
select '225'
go

select nob from(
select * from (select nob from t1)b cross apply (select ltrim(noe)a from t2) ar
)temp
where charindex(a,nob)>0

drop table t1,t2

nob
----------
LSV123
LBV126
LCV225

(3 行受影响)
飘零一叶 2011-01-14
  • 打赏
  • 举报
回复
create table t1
(
no varchar(10)
)
create table t2
(
no varchar(10)
)
insert into t1
select 'LSV123' union all
select 'LBV124' union all
select 'LBV126' union all
select 'LCV225' union all
select 'DVB630'

insert into T2
select '123' union all
select '126' union all
select '225'


select t1.* from t1 join T2 on CHARINDEX(T2.no,T1.no)>0

/*------------------
no
LSV123
LBV126
LCV225
*/

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧