很久没来,问一问题,顺便放点血

zhangzs8896 2005-09-27 08:13:39
有两个表:
Sys_WFStatistics和Sys_Emplyee
其中Sys_WFStatistics中有字段StatisticsID,int类型
Sys_Emplyee中有字段StatisticsReports 类型varchar,存放: "'1','2','3'"
现在需要找出Sys_WFStatistics中StatisticsID在StatisticsReports中的记录
我这样写的:
select SortID,StatisticsID,StatisticsName,StatisticsUrl,Remark from Sys_WFStatistics where ''+Convert(varchar,StatisticsID)+'' in ( select StatisticsReports from Sys_Emplyee where EmpAccount='NEWSIGHT\zhangzhongsheng')

查询不到数据.
但是这样
select SortID,StatisticsID,StatisticsName,StatisticsUrl,Remark from Sys_WFStatistics where ''+Convert(varchar,StatisticsID)+'' in ( '1','2','3' )
是可以的.

寻求解决办法,效率越高越好!


...全文
62 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangzs8896 2005-09-27
  • 打赏
  • 举报
回复
呵呵,已经搞定了,方法跟 vivianfdlpw()差不多的.
vivianfdlpw 2005-09-27
  • 打赏
  • 举报
回复
create table Sys_WFStatistics
(
StatisticsID ID,
SortID int default 0,
StatisticsName varchar(20) default '',
StatisticsUrl varchar(20) default '',
Remark varchar(100) default ''
)

create table Sys_Emplyee
(
StatisticsReports varchar(20),
EmpAccount varchar(100)
)
insert Sys_WFStatistics(StatisticsID)
select 1 union
select 2 union
select 3
insert Sys_Emplyee
select '''1'',''2'',''3''','NEWSIGHT\zhangzhongsheng'
go


--查询
select SortID
,StatisticsID
,StatisticsName
,StatisticsUrl
,Remark
from Sys_WFStatistics
where exists (select 1
from Sys_Emplyee
where EmpAccount='NEWSIGHT\zhangzhongsheng'
and charindex(','''+Convert(varchar,StatisticsID)+''','
,
','+StatisticsReports+',')>0
)

drop table Sys_WFStatistics,Sys_Emplyee


/*--结果
SortID StatisticsID StatisticsName StatisticsUrl Remark
----------- ------------ -------------------- -------------------- --------
0 1
0 2
0 3

(3 row(s) affected)

--*/
vivianfdlpw 2005-09-27
  • 打赏
  • 举报
回复
create table Sys_WFStatistics
(
StatisticsID ID,
SortID int default 0,
StatisticsName varchar(20) default '',
StatisticsUrl varchar(20) default '',
Remark varchar(100) default ''
)

create table Sys_Emplyee
(
StatisticsReports varchar(20),
EmpAccount varchar(100)
)
insert Sys_WFStatistics(StatisticsID)
select 1 union
select 2 union
select 3
insert Sys_Emplyee
select '''1'',''2'',''3''','NEWSIGHT\zhangzhongsheng'
go


--查询
select SortID
,StatisticsID
,StatisticsName
,StatisticsUrl
,Remark
from Sys_WFStatistics
where exists (select 1
from Sys_Emplyee
where EmpAccount='NEWSIGHT\zhangzhongsheng'
and charindex(','''+Convert(varchar,StatisticsID)+''','
,
','''+StatisticsReports+''',')>0
)

drop table Sys_WFStatistics,Sys_Emplyee


/*--结果
SortID StatisticsID StatisticsName StatisticsUrl Remark
----------- ------------ -------------------- -------------------- -------
0 2

(1 row(s) affected)

--*/
vivianfdlpw 2005-09-27
  • 打赏
  • 举报
回复
try:

select SortID
,StatisticsID
,StatisticsName
,StatisticsUrl
,Remark
from Sys_WFStatistics
where exists (select 1
from Sys_Emplyee
where EmpAccount='NEWSIGHT\zhangzhongsheng'
and charindex(','''+Convert(varchar,StatisticsID)+''','
,
','''+StatisticsReports+''',')>0
)
zhangzs8896 2005-09-27
  • 打赏
  • 举报
回复
不行,类型转换不过去的.
2楼哥们结果跟我一样,无记录
wgsasd311 2005-09-27
  • 打赏
  • 举报
回复
或者
select SortID,StatisticsID,StatisticsName,StatisticsUrl,Remark from Sys_WFStatistics where StatisticsID in ( select cast(StatisticsReports as varchar) from Sys_Emplyee where EmpAccount='NEWSIGHT\zhangzhongsheng')
vivianfdlpw 2005-09-27
  • 打赏
  • 举报
回复
select SortID
,StatisticsID
,StatisticsName
,StatisticsUrl
,Remark
from Sys_WFStatistics
where exists (select 1
from Sys_Emplyee
where EmpAccount='NEWSIGHT\zhangzhongsheng'
and charindex(','+Convert(varchar,StatisticsID)+','
,
','+StatisticsReports+',')>0
)
wgsasd311 2005-09-27
  • 打赏
  • 举报
回复
select SortID,StatisticsID,StatisticsName,StatisticsUrl,Remark from Sys_WFStatistics where StatisticsID in ( select StatisticsReports from Sys_Emplyee where EmpAccount='NEWSIGHT\zhangzhongsheng')

34,576

社区成员

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

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