有两个表
A表 结构如下 id name
1 篮球
11 足球
111 排球 B表 结构如下 id 善长球类列表 name
1 1,11,111 小飞
2 1,11 张三
3 1 李四 B表中善长球类列表中的1,11,111均为表A中所来我如何查询B表中善长球类列表中含11值的列表输入值11查询结果如下:id 善长球类列表 name
1 1,11,111 小飞
2 1,11 张三
A表 结构如下 id name
1 篮球
11 足球
111 排球 B表 结构如下 id 善长球类列表 name
1 1,11,111 小飞
2 1,11 张三
3 1 李四 B表中善长球类列表中的1,11,111均为表A中所来我如何查询B表中善长球类列表中含11值的列表输入值11查询结果如下:id 善长球类列表 name
1 1,11,111 小飞
2 1,11 张三
set @s='11'
select * from b where charindex(','+@s+',',','+善长球类列表+',')>0
from a,b
where
charindex(','+ltrim(a.id)+',',','+b.善长球类列表+',')>0
and
a.id=11
declare @s varchar(100)
set @s='11'
select * from b where charindex(','+@s+',',','+善长球类列表+',')>0
up
/*---------------------------------
-- Author : htl258(Tony)
-- Date : 2009-09-05 10:21:39
-- Version: Microsoft SQL Server 2005 - 9.00.3077.00 (Intel X86)
Dec 17 2008 15:19:45
Copyright (c) 1988-2005 Microsoft Corporation
Developer Edition on Windows NT 6.0 (Build 6001: Service Pack 1)---------------------------------*/
--> 生成测试数据表:bIf not object_id('[b]') is null
Drop table [b]
Go
Create table [b]([id] int,[善长球类列表] nvarchar(20),[name] nvarchar(2))
Insert [b]
Select 1,'1,11,111',N'小飞' union all
Select 2,'1,11',N'张三' union all
Select 3,'1',N'李四'
Go
--Select * from [b]-->SQL查询如下:
declare @s varchar(100)
set @s='11'
select * from b where charindex(','+@s+',',','+善长球类列表+',')>0
/*
id 善长球类列表 name
----------- -------------------- ----
1 1,11,111 小飞
2 1,11 张三(2 行受影响)
*/
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-05 10:24:35
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([id] int,[name] varchar(4))
insert [A]
select 1,'篮球' union all
select 11,'足球' union all
select 111,'排球'
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([id] int,[善长球类列表] varchar(8),[name] varchar(4))
insert [B]
select 1,'1,11,111','小飞' union all
select 2,'1,11','张三' union all
select 3,'1','李四'
--------------开始查询--------------------------
declare @s varchar(100)
set @s='11'
select * from b where charindex(','+@s+',',','+善长球类列表+',')>0----------------结果----------------------------
/*id 善长球类列表 name
----------- -------- ----
1 1,11,111 小飞
2 1,11 张三(2 行受影响)
*/
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-05 10:24:35
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([id] int,[name] varchar(4))
insert [A]
select 1,'篮球' union all
select 11,'足球' union all
select 111,'排球'
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([id] int,[善长球类列表] varchar(8),[name] varchar(4))
insert [B]
select 1,'1,11,111','小飞' union all
select 2,'1,11','张三' union all
select 3,'1','李四'
--------------开始查询--------------------------
select
b.*
from
a,b
where
charindex(','+ltrim(a.id)+',',','+b.善长球类列表+',')>0
and
a.id=11
----------------结果----------------------------
/*id 善长球类列表 name
----------- -------- ----
1 1,11,111 小飞
2 1,11 张三(2 行受影响)
*/
Drop table [b]
Go
Create table [b]([id] int,[善长球类列表] nvarchar(20),[name] nvarchar(2))
Insert [b]
Select 1,'1,11,111',N'小飞' union all
Select 2,'1,11',N'张三' union all
Select 3,'1',N'李四'
Goselect * from b where [善长球类列表] like '%11%'/*
id 善长球类列表 name
1 1,11,111 小飞
2 1,11 张三
*/select * from b where [善长球类列表] like '%1%'
/*
id 善长球类列表 name
1 1,11,111 小飞
2 1,11 张三
3 1 李四
*/
select * from b where [善长球类列表] like '%111%'
/*
id 善长球类列表 name
1 1,11,111 小飞
*/干嘛!想这么复杂!这样最简单!
这是不对的,你用 like '%11%' 一样可以把 4 王五 1111 查出来,而事实上它是没有11的
declare @t table(id int, l varchar(20), name varchar(10))
insert into @t
select 1, '1,11,111', '小飞'
union all select 2, '1,11', '张三'
union all select 3, '1', '李四'
select * from @t
where charindex('11',l)>0id l name
----------- -------------------- ----------
1 1,11,111 小飞
2 1,11 张三(所影响的行数为 2 行)
set @s='11'
select * from b where charindex(','+@s+',',','+善长球类列表+',')>0ordeclare @s varchar(100)
set @s='11'
select * from b where ','+善长球类列表+',' like '%,'+@s+',%'
id 善长球类列表 name
1 11,111 小飞
2 11 张三
3 1 李四
又如何取呢?
Drop table [b]
Go
Create table [b]([id] int,[善长球类列表] nvarchar(20),[name] nvarchar(2))
Insert [b]
Select 1,'11,111',N'小飞' union all
Select 2,'11',N'张三' union all
Select 3,'1',N'李四'
Go
--Select * from [b]-->接分
declare @s varchar(100)
set @s='11'
select * from b where charindex(','+@s+',',','+善长球类列表+',')>0
/*
id 善长球类列表 name
----------- -------------------- ----
1 11,111 小飞
2 11 张三(2 行受影响)*/