求一条sql语句的思路,高手过来啊

不老书生 2005-06-07 10:50:19
一张部门表,department,部门编号字段Dcode,分多级的

一张人员表,employee表,人员编号ucode和部门编号dcode

每次我需要查分布在某若干个部门的人员,我能够先程序得到这些部门编号的列表,10,22,33,21

然后我的做法是select * from employee where Dcode in ('"&Dcodelist&"') ,其中Dcodelist就是
10,22,33...

现在有两个问题,一个是这样的用in语句,效率是不是比较低?

第二,我的部门编号Dcode是用户手输的,可以是任意字母数字组合,系统只做唯一性判断,于是当某个部门编号是字符串时,如ytsw100时,
select * from employee where Dcode in (10,11,ytsw100),就会报错,说列名ytsw100无效。是不是in()括号里只能是数字啊?
...全文
142 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kendo7 2005-06-07
  • 打赏
  • 举报
回复
Dim sqlStr
Dim Dcodelist
Dcodelist=Array(10,20,40)
sqlStr=""
for i=0 to ubound(Dcodelist)
if i=ubound(Dcodelist) then
sqlStr=sqlStr&"select a.* from employee where Dcode="& Dcodelist(i)
else
sqlStr=sqlStr&"select a.* from employee where Dcode="& Dcodelist(i) & " union "
end if
next
king2410 2005-06-07
  • 打赏
  • 举报
回复
in(数字,数字,数字)
or
in('字符','字符','字符')
zlhran 2005-06-07
  • 打赏
  • 举报
回复
do each ...in docodelist..
...
select * from....
..
如何??
jybbh 2005-06-07
  • 打赏
  • 举报
回复
第二个问题:
select * from employee where Dcode in (10,11,ytsw100),
改成
select * from employee where Dcode in (10,11,'ytsw100'),
不是数字的,加上单引号
若白师妹 2005-06-07
  • 打赏
  • 举报
回复
都5条内裤了,难?
cheng17 2005-06-07
  • 打赏
  • 举报
回复
1、MSSQL用charindex,AC库用instr

sql="select GoodID,Gname,Gmodel from Goods where InStr(GClass,'A')>0 or InStr(GClass,'B')>0 order by GIns_Date desc"

2、数字型 ,字符型放在in里面肯定会出错。
kknd3273 2005-06-07
  • 打赏
  • 举报
回复
select * from employee where Dcode in (10,11,ytsw100)
这样好像是 类型不匹配了 in()括号里只能是数字 中间用“逗号”隔开



用in语句,效率是不是比较低?这个我也不知道
我也是才用 IN 写的 SQL 语句

不老书生 2005-06-07
  • 打赏
  • 举报
回复
ding
不老书生 2005-06-07
  • 打赏
  • 举报
回复
我必须要用一条sql语句

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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