+ -
当前位置:首页 → 问答吧 → 这是个难度高的问题,请高手帮忙搞定?

这是个难度高的问题,请高手帮忙搞定?

时间:2010-11-14

来源:互联网

这个问题很麻烦,都知道UPDATE 只能同时用于一个表的更新,但我这里需要根据前面的值STR,来进行判断,如果符合条件1(结果有表1的字段)就要UPDATE表1,如符合条件2(有表2的字段)就要UPDATE 表2,如果符合条件3(结果有表3的字段)就要UPDATE表3。
我把前面的程序也放在这,大家帮忙看看如何写SQL这句子:
myfriendpic是表 myfriend 项, giftimage是表 gift项, gamepath是表 game项
.......
 VBScript code
sql="SELECT A.myfriendpic,B.giftimage,C.gamepath FROM myfriend A,gift B,game C WHERE A.uid='"&uid&"' AND B.uid='"&uid&"' AND C.uid='"&uid&"'  AND (A.myfriendpic LIKE '%"&src&"%' or B.giftimage LIKE '%"&src&"%' or C.gamepath LIKE '%"&src&"%')"
set rs=conn.execute(sql)
 dim str
 do while not rs.eof
  myfriendpic = rs(0) & ""  
  giftimage = rs(1) & ""   
  gamepath = rs(2) & ""   

  myfriendpic=DealCharactor(myfriendpic,0)
  giftimage=DealCharactor(giftimage,0)
  gamepath=DealCharactor(gamepath,0)
  uid=""&uid&""
  src=DealCharactor(src,0)
  if NeedUpdate(src, myfriendpic) then str = "SET myfriendpic='" & DealCharactor(Replace(myfriendpic, src, ""), 1) & "'"
  if NeedUpdate(src, giftimage) then
  if str = "" then
  str = "SET giftimage='" & DealCharactor(Replace(giftimage, src, ""), 1) & "'"
  else
  str = str & ",giftimage='" & DealCharactor(Replace(giftimage, src, ""), 1) & "'"
  end if
  end if
  if NeedUpdate(src, gamepath) then
  if str = "" then
  str = "SET gamepath='" & DealCharactor(Replace(gamepath, src, ""), 1) & "'"
  else
  str = str & ",gamepath='" & DealCharactor(Replace(gamepath, src, ""), 1) & "'"
  end If
  end If
......

sql=不知咋写?

conn.execute sql
rs.movenext
  loop

作者: frankrenping   发布时间: 2010-11-14

本打算把这张帖去除了,但要24小时才能去掉。
其实想想也不能在一条路上走死,我改成调用几次不同的后台程序来做多次去除图片这件事,但还是遇到了问题,

整个程序如下:看看谁能知道错在哪里?
<%
 Response.CharSet="gb2312"
 flag=Request.QueryString("flag")
  uid = request.querystring("uid")
if len(flag)>0 then
  if flag="deleteImg" then  
  on error resume next
  src=Request.QueryString("src")
 err.clear
  conn.BeginTrans  
sql="SELECT myfriendpic FROM myfriend WHERE uid='"&uid&"' AND (myfriendpic LIKE '%"&src&"%')"

 set rs=conn.execute(sql)
  dim str
do while not rs.eof
  myfriendpic = rs(0) & "" 
myfriendpic=DealCharactor(myfriendpic,0)
uid=""&uid&""
  src=DealCharactor(src,0)
 if NeedUpdate(src, myfriendpic) then 
str = "" & DealCharactor(Replace(myfriendpic, src, ""), 1) & ""

if len(str)>0 then
sql="UPDATE [myfriend] set [myfriendpic]="&str&" WHERE uid="&uid&""
conn.execute sql
end if  
end if  
rs.movenext
  loop
 if conn.errors.count=0 and err=0 then
  conn.CommitTrans
  msg="delete succeed!"
  else  
  conn.RollbackTrans
  msg="delete fail!@error:"&err.description
  end if
  set rs=nothing
  conn.close: set conn=nothing
  if err=0 then msg="delete succeed!" else msg="删除图片时出现错误!@error:"&err.description
  Response.Write msg: Response.End 
  end if
  end if
  
Function NeedUpdate(src, sFieldData) 
  NeedUpdate = False
  If Len(sFieldData) < 5 Then Exit Function
  If InStr(sFieldData,src ) > 0 Then NeedUpdate = True
End Function

function DealCharactor(byval s,byval flag)
  s=trim(s)
  select case flag
  case 0
  if left(s,1)<>"," then s=","&s
  if right(s,1)<>"," then s=s&","
  case 1
  if left(s,1)="," then s=right(s,len(s)-1)
  if right(s,1)="," then s=left(s,len(s)-1)
  end select
  DealCharactor=s
end function  
%>

作者: frankrenping   发布时间: 2010-11-15

大致理解了下。。

VBScript code

STR=trim(request("str"))     '首先获取页面传过来的值
CaseTable=""        '设一个变量,用来确认传过来的值是表1的还是表2的还是表3的
set rs=server.creatObject("ADODB.Recordset")

sql="select * from 表1 where 表1字段 like '%"&STR&"%'"
rs.open sql,conn,1,1
if not rs.eof or rs.bof then
CaseTable=1       '这里表示这个字段是属于表1的
end if
rs.close

sql="select * from 表2 where 表2字段 like '%"&STR&"%'"
rs.open sql,conn,1,1
if not rs.eof or rs.bof then
CaseTable=2      '这里表示这个字段是属于表2的
end if
rs.close

sql="select * from 表3 where 表3字段 like '%"&STR&"%'"
rs.open sql,conn,1,1
if not rs.eof or rs.bof then
CaseTable=3      '这里表示这个字段是属于表3的
end if
rs.close

'response.write(CaseTable)   '这样我们就知道你传过来的值是属于哪个表了

'然后我们就能根据CaseTable的值去决定UPDATE哪个表了
if CaseTable=1 then
    sql="Update 表1 ....."
elseif CaseTable=2 then
    sql="Update 表2 ....."
elseif CaseTbale=3 then
    sql="Update 表3 ....."
end if

'response.write(sql)  得到相应的SQL语句
Conn.execute(sql)    '执行相应的SQL语句

作者: tcwsyt   发布时间: 2010-11-15

手打的难免有错。
还有就是如果传过来的值同时符合多个条件。不知LZ想如何处理。

作者: tcwsyt   发布时间: 2010-11-15

热门下载

更多