把查询语句赋值给string变量,在吧string赋值给其余重复的部分

lioujrbang 2016-11-26 11:57:16
把查询语句赋值给string变量,在把string赋值给其余重复的部分,
下面查询有很多字符串重复的部分,

select Id ,Tid as 类型,Title as 标题,Cover as 图片
from Video as A where Tid in
(select Id from VideoType where tid in
(select Id from VideoType where tid in
(select Id from VideoType where tid in
(select Id from VideoType where tid in
(select id from VideoType where tid in(select id from VideoType where Title in('老人专区','幼儿专区')))))))
or Tid in
(select Id from VideoType where tid in
(select Id from VideoType where tid in
(select Id from VideoType where tid in
(select id from VideoType where tid in(select id from VideoType where Title in('老人专区','幼儿专区'))))))
or tid in
(select Id from VideoType where tid in
(select Id from VideoType where tid in
(select id from VideoType where tid in (select id from VideoType where Title in('老人专区','幼儿专区')))))
or tid in
(select Id from VideoType where tid in
(select id from VideoType where tid in (select id from VideoType where Title in('老人专区','幼儿专区'))))
or tid in
(select id from VideoType where tid in (select id from VideoType where Title in('老人专区','幼儿专区')))
or tid in (select id from VideoType where Title in('老人专区','幼儿专区'))

能不能这样 string a=" (select Id from VideoType where tid in"
然后把 a 一个个都接上去,
这样SQL整体就缩小了很多.

select Id ,Tid as 类型,Title as 标题,Cover as 图片
from Video as A where Tid in
a
a
a
a
a(select id from VideoType where Title in('老人专区','幼儿专区')))))))
or Tid in
a
a
a
a(select id from VideoType where Title in('老人专区','幼儿专区'))))))
or tid in
a
a
a (select id from VideoType where Title in('老人专区','幼儿专区')))))
or tid in
a
a (select id from VideoType where Title in('老人专区','幼儿专区'))))
or tid in
a(select id from VideoType where Title in('老人专区','幼儿专区')))
or tid in (select id from VideoType where Title in('老人专区','幼儿专区'))
...全文
208 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lioujrbang 2016-11-26
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:
可以考虑把 a 放在一个 with as 中。
原语句 select id from VideoType where tid in(select id from VideoType where Title in('老人专区','幼儿专区')) 这样写报错列名B无效, WITH B AS (select id from VideoType where Title in('老人专区','幼儿专区')) select id from VideoType where tid in(B) 这样写可以,但我要的不是这个是上面那个效果 WITH B AS (select id from VideoType where Title in('老人专区','幼儿专区')) select id from B 怎么办版主大大
卖水果的net 版主 2016-11-26
  • 打赏
  • 举报
回复
可以考虑把 a 放在一个 with as 中。
卖水果的net 版主 2016-11-26
  • 打赏
  • 举报
回复
引用 2 楼 u012533386 的回复:
这样写报错列名B无效, WITH B AS (select id from VideoType where Title in('老人专区','幼儿专区')) select id from VideoType where tid in(B) 这样写可以,但我要的不是这个是上面那个效果 WITH B AS (select id from VideoType where Title in('老人专区','幼儿专区')) select id from B 怎么办版主大大
那就只能先把这些数据查询出来,放在一个长字符串中,最后拼接到最后这个大 SQL 中了;

34,590

社区成员

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

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