这样的sql语句怎么写?

gmj_1019 2004-11-21 03:00:04
表:test 如下所示
t_id(主键),t_mc, t_jh
1 001 1,2,3,4
2 002 1,2,3
3 003 1,2
4 004 2,3
5 005 5
现在我想实现:
id的值是1,满足表test中t_jh内含有id值的记录全部显示出来,这样的sql怎么写?
即应显示第一条,第二条,第三条信息
谢谢!
...全文
171 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
davorsuker39 2004-11-22
  • 打赏
  • 举报
回复
UP
prcgolf 2004-11-22
  • 打赏
  • 举报
回复
UP
肥仔胧 2004-11-21
  • 打赏
  • 举报
回复
Mark
zanyzyg 2004-11-21
  • 打赏
  • 举报
回复

这个不就是只有两种情况吗

1、 xx,
2、 ,xx

你就找含有“xx,”或者“,xx”的就可以了啊。



wukele 2004-11-21
  • 打赏
  • 举报
回复
select * from t where t_jh like '%,'+t_id+',%' or t_jh like t_id+',%' 我是新手,不知道这样写可以吗?符合题目要求吗/
gmj_1019 2004-11-21
  • 打赏
  • 举报
回复
charindex()函数在access里是否使用?
gmj_1019 2004-11-21
  • 打赏
  • 举报
回复
我看,我还是把t_jh中字符串格式化。原来的1,2,3变成001,002,003
最后用charindex(exp1,exp2)>0来实现即可
zlp321002 2004-11-21
  • 打赏
  • 举报
回复
那就用那函数吧!!应该没有问题的!!!
gmj_1019 2004-11-21
  • 打赏
  • 举报
回复
TO:fenlin(孤帆)
如果t_jh中有11,12,之类的也显示出来了
zlp321002 2004-11-21
  • 打赏
  • 举报
回复
select * from test where charindex(','+'%[cast(1 as varchar)]%'+',',','+t_jh+',')>0
gmj_1019 2004-11-21
  • 打赏
  • 举报
回复
t_jh是我存的id集合,用逗号分隔,
但是我利用 select * from test where 1 in (t_jh)
提示我类型不符合。
zlp321002 2004-11-21
  • 打赏
  • 举报
回复
TO: chd2001(天蝎降临)
单charindex('%[1]%',t_jh)>0 --中的'1'是t_id(主键) 传递进去的啊!!
如果t_id(主键)中有111 就可以啊!

不过,最好用函数!!,要具体问题具体分析。关键这个'1' 的得到问题。。。
fenlin 2004-11-21
  • 打赏
  • 举报
回复
declare @i int
set @i=1
select * from a where patindex('%'+cast(@i as varchar)+'%',t_jh)>0
zlp321002 2004-11-21
  • 打赏
  • 举报
回复
select * from test where charindex('%[1]%',t_jh)>0
帮忙测试下,可不可以??
Andy__Huang 2004-11-21
  • 打赏
  • 举报
回复
--示例


create table tab1(CompanyPropertyID varchar(10),CompanyPropertyName varchar(100))
Insert into tab1
select '01','aaa'
union all select '02','bbb'
union all select '03','ccc'

create table tab2(SellID varchar(10),CompanyPropertyIDS varchar(100))
Insert into tab2
select 'A001','01;02;'
union all select 'A001','01'
union all select 'A001','01;03;'
union all select 'A001','01;02;03;'

select * from tab1
select * from tab2

--創建函數處理
create function dbo.fn_f(@s varchar(1000))
returns varchar(1000)
as
begin
declare @a varchar(1000)
declare @i int
set @a=''
set @i=1
while @i>0
begin
select @a=@a+CompanyPropertyName+';' from tab1 where CompanyPropertyID=substring(@s,@i,2)
set @i=charindex(';',@s,@i+3)-2
end
return(left(@a,len(@a)-1))
end

--刪除
drop table tab1
drop table tab2
drop function dbo.fn_f

--結果
select SellID,CompanyPropertyName=dbo.fn_f(CompanyPropertyIDS) from tab2
SellID CompanyPropertyName
---------------------------------------------
A001 aaa;bbb
A001 aaa
A001 aaa;ccc
A001 aaa;bbb;ccc
Andy__Huang 2004-11-21
  • 打赏
  • 举报
回复
這個要用函數處理

34,590

社区成员

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

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