如何查询某个字段包含某个字符?

yasire 2009-12-18 11:14:51
比如:
name
1,2,3
12,13,14
4,5

查询表tb中name包含2的记录。
注意:如果select * from tb where name like '%2%'是不正确的,因为他会把12,13,14这条也查出来。
我需要的是数组包含的关系。
...全文
115 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2009-12-18
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-18 11:16:56
-- 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)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(8))
insert [tb]
select '1,2,3' union all
select '12,13,14' union all
select '4,5'
--------------开始查询--------------------------
select * from tb where charindex(','+'2'+',',','+name+',')>0
----------------结果----------------------------
/* name
--------
1,2,3

(1 行受影响)
*/
--小F-- 2009-12-18
  • 打赏
  • 举报
回复
select * from tb where charindex(','+'2'+',',','+name+',')>0
dawugui 2009-12-18
  • 打赏
  • 举报
回复
select * from tb where ',' + name + ',' like '%,2,%'

select * from tb where charindex(',2,' , ',' + name + ',') > 0

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧