日期:2014-05-18 浏览次数:20498 次
select * from b where col like '%time sheet%'
------解决方案--------------------
select * from b where col like '%time%sheet% '
------解决方案--------------------
如a表中有time sheet b表中有time sheet is a table、time sheet 123、 如果上述内容为一条记录 select b.* from b,a where charindex(a.col , b.col) > 0
------解决方案--------------------
declare @b table(col varchar(32)) insert @b select 'time sheet is a table' union all select 'time sheet 123' union all select 'time 123 sheet' select * from @b where col like '%time sheet%' /* col -------------------------------- time sheet is a table time sheet 123 (2 row(s) affected) */
------解决方案--------------------
declare @b table(col varchar(max)) insert @b select 'time sheet is a table' union all select 'time sheet 123' union all select 'time 123 sheet' declare @keyword varchar(32) -- select @keyword=keyword from A where ... /*如果关键字从A表中查,用这句*/ set @keyword='time sheet' while charindex(' ',@keyword)>=1 set @keyword=replace(@keyword,' ','') set @keyword=replace(@keyword,' ','%') select * from @b where col like '%'+@keyword+'%' /* col ------------------------------------ time sheet is a table time sheet 123 time 123 sheet (3 row(s) affected) */
------解决方案--------------------
如果a表中time sheet是一行, 并且要求能查出 time 123 sheet
那么14楼的写法就可以了.
如果不是一行
DECLARE @a TABLE(id INT IDENTITY(1,1),v VARCHAR(20)) INSERT @a SELECT 'time' UNION SELECT 'sheet' DECLARE @b TABLE(id INT IDENTITY(1,1),v VARCHAR(100)) INSERT @b SELECT 'time sheet is a table' UNION SELECT 'time sheet 123' UNION SELECT 'abc time 123 sheet' UNION SELECT 'time' UNION SELECT 'asbc 123 sheet xxx' SELECT DISTINCT b.* FROM @b b INNER JOIN @a a ON b.v LIKE '%' + a.v + '%' AND NOT EXISTS(SELECT 1 FROM @a WHERE b.v NOT LIKE '%' + v + '%')