判断某几列里是否存在某个字符串,语句要如何写呢?

angleoldhen 2007-12-05 04:12:55
页面已经获取session["username"],现在要找出数据表里某3列字段包含该用户名的记录,如何写呢?

比如:
字段名:id agroup bgroup cgroup

记录: 1 a,b c b,d
2 a b c

假设 session["username"]=d,那么我想取到第一条记录要怎么写呢?
...全文
161 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2007-12-05
  • 打赏
  • 举报
回复
declare @name varchar(100)
set @name = 'd'
select top 1 *
from table
where charindex(','+@name+',',','+agroup+','+bgroup+','+cgroup + ',') > 0
order by id
-狙击手- 2007-12-05
  • 打赏
  • 举报
回复
这样通用吗?
declare @name varchar(100)
set @name = 'd'
select top 1 *
from table
where charindex(',+@name+,',','+agroup+','+bgroup+','+cgroup + ',') > 0
order by id
andy84 2007-12-05
  • 打赏
  • 举报
回复
set @session='d'
andy84 2007-12-05
  • 打赏
  • 举报
回复
+@session
因为四楼的写法是变量赋值的
你前面加个
set @session=''d''
angleoldhen 2007-12-05
  • 打赏
  • 举报
回复
4楼的写法通不过啊
andy84 2007-12-05
  • 打赏
  • 举报
回复
不错
受益
nolast02 2007-12-05
  • 打赏
  • 举报
回复
正解
路过
smapple 2007-12-05
  • 打赏
  • 举报
回复
以上均正解。我就不写啦。:)
-狙击手- 2007-12-05
  • 打赏
  • 举报
回复
select top 1 *
from table
where charindex(',d,',','+agroup+','+bgroup+','+cgroup + ',') > 0
order by id
dawugui 2007-12-05
  • 打赏
  • 举报
回复
--其中一个包含.
select top 1 * from tb where charindex(','+@session+',' , ','+agroup+',') > 0 or
charindex(','+@session+',' , ','+bgroup+',') > 0 or
charindex(','+@session+',' , ','+cgroup+',') > 0
--三个都包含
select top 1 * from tb where charindex(','+@session+',' , ','+agroup+',') > 0 and
charindex(','+@session+',' , ','+bgroup+',') > 0 and
charindex(','+@session+',' , ','+cgroup+',') > 0
-狙击手- 2007-12-05
  • 打赏
  • 举报
回复
select top 1 *
from table
where charindex(',d,',','+agroup+',') > 0 or charindex(',d,',','+bgroup+',') > 0
or charindex(',d,',','+cgroup+',') > 0
order by id
dawugui 2007-12-05
  • 打赏
  • 举报
回复
select top 1 * from tb where charindex(','+@session+',' , ','+agroup+',') > 0 or
charindex(','+@session+',' , ','+bgroup+',') > 0 or
charindex(','+@session+',' , ','+cgroup+',') > 0

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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