日期:2014-05-17 浏览次数:20505 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-01-21 17:16:18
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([id] int,[name1] varchar(4))
insert [a]
select 1,'線' union all
select 2,'掛牌' union all
select 3,'紙箱' union all
select 4,'貼紙'
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([id] int,[name2] varchar(8))
insert [b]
select 1,'501線' union all
select 2,'502線' union all
select 3,'503線' union all
select 4,'膠袋貼紙' union all
select 5,'紙箱貼紙'
--------------开始查询--------------------------
select a.id,a.name1,b.name2
from [a] full JOIN [b] ON CHARINDEX(name1,name2,1)>0
WHERE b.id IS NOT NULL
ORDER BY a.id
----------------结果----------------------------
/*
id name1 name2
----------- ----- --------
1 線 501線
1 線 502線
1 線 503線
3 紙箱 紙箱貼紙
4 貼紙 紙箱貼紙
4 貼紙 膠袋貼紙
*/