请教

会飞的小洋洋 2007-08-01 10:55:55
表a
id content
1 11,22,33,44,55,66
2 77,88,99

----------代码
declare @content varchar(200)
select top 1 @content=content from a
select * from orders where content in(@content)

----------如何实现以下查询的结果
select * from others where id in(11,22,33,44,55,66)
...全文
125 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
paoluo 2007-08-01
  • 打赏
  • 举报
回复
--或者Like
Select T1.* from orders T1
Inner Join a T2
On ',' + T2.content + ',' Like '%,' + Cast(T1.id As Varchar) + ',%'
Where T2.id = (Select TOP 1 id From a)
paoluo 2007-08-01
  • 打赏
  • 举报
回复
--改用CharIndex,可以不用使用動態SQL語句
Select T1.* from orders T1
Inner Join a T2
On CharIndex(',' + Cast(T1.id As Varchar) + ',', ',' + T2.content + ',') > 0
Where T2.id = (Select TOP 1 id From a)
$扫地僧$ 2007-08-01
  • 打赏
  • 举报
回复
create table A
(
id int,
content varchar(100)
)

insert A select 1,'11,22,33,44,55,66'
insert A select 2,'77,88,99'

create table B
(
T int
)
insert B select 11
insert B select 22
insert B select 88

select * from B where charindex(cast(T as varchar),(select top 1 content from A ))>0
昵称被占用了 2007-08-01
  • 打赏
  • 举报
回复
declare @content varchar(200)
select top 1 @content=content from a
exec('select * from orders where id in('+@content+')')
ojuju10 2007-08-01
  • 打赏
  • 举报
回复
declare @content varchar(200)
select top 1 @content=content from a
exec('select * from orders where content in('+@content+')')
paoluo 2007-08-01
  • 打赏
  • 举报
回复
用了動態語句了?!這種情況沒有必要使用動態語句的。

34,590

社区成员

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

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