34,576
社区成员
发帖
与我相关
我的任务
分享
--貌似再mysql中字符串拼接是用concat函数。
--如果是用like就是
select * from @tablea a LEFT JOIN @tableb b
on a.col LIKE CONCAT('%',b.col,'%')
WHERE a.col LIKE '%ff%' OR a.col LIKE '%ie%'
--要用in就是
select * from @tablea a LEFT JOIN @tableb b
on a.col LIKE CONCAT('%',b.col,'%')
WHERE a.col in ('ff','ie')
--a表
declare @tablea table (id int,col varchar(3))
insert into @tablea
select 1,'aaa' union all
select 2,'bbb' union all
select 3,'ccc' union all
select 4,'ddd' union all
select 5,'eee' union all
select 6,'fff'
--b表
declare @tableb table (id int,col varchar(4))
insert into @tableb
select 10,'aa' union all
select 11,'bba' union all
select 12,'ccd' union all
select 13,'ddde' union all
select 14,'ee' union all
select 15,'ff'
--联查
select * from @tablea a LEFT JOIN @tableb b
on a.col LIKE '%'+b.col+'%'
/*
id col id col
----------- ---- ----------- ----
1 aaa 10 aa
2 bbb NULL NULL
3 ccc NULL NULL
4 ddd NULL NULL
5 eee 14 ee
6 fff 15 ff
*/
--加上条件
select * from @tablea a LEFT JOIN @tableb b
on a.col LIKE '%'+b.col+'%'
WHERE a.col LIKE '%ff%' OR a.col LIKE '%ie%'
/*
id col id col
----------- ---- ----------- ----
6 fff 15 ff
*/
SELECT A.name
FROM A LEFT JOIN B
ON CHARINDEX(B.[name], A.[name]) > 0
WHERE A.name IN('ff', 'ie')