日期:2014-05-17 浏览次数:20467 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-01-14 20:46:05
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
-- Jun 17 2011 00:57:23
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([rowidA] int,[nameA] 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]([rowidB] int,[nameB] varchar(4),[rowidA] varchar(3))
insert [B]
select 1,'水果','1,2' union all
select 2,'食品','3,4'
--------------开始查询--------------------------
SELECT b.[rowidB] ,
b.nameB ,
a.nameA
FROM [A] a
INNER JOIN ( SELECT [rowidB] ,
[nameB] ,
SUBSTRING([rowidA], number,
CHARINDEX(',', [rowidA] + ',', number)