标题:[求助]SQL语句中怎样同时查询一个表中的两个字段
取消只看楼主
gmqq
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-2
 问题点数:0 回复次数:8 
[求助]SQL语句中怎样同时查询一个表中的两个字段

问题:SQL语句中怎样同时查询一个表中的两个字段

相关源码:Sql="Select * From MovieList where title like '%"&keyword&"%' Order BY ID Desc"
title是标题名的字段,我想在增加一个产品内容的content字段,都在同一个表内

Dim Page
Page=Request("Page")
PageSize = 15
Dim Rs,Sql
If ClassID="" Then
Sql="Select * From MovieList where title like '%"&keyword&"%' Order BY ID Desc"
Else
Sql="Select * From MovieList where content like '%"&keyword&"%' Order BY ID Desc"
End if
Set Rs=Server.CreateObject("ADODB.Recordset")
Rs.open Sql,Conn,3,3
Rs.PageSize = PageSize
Totalfilm=Rs.RecordCount
Pgnum=Rs.Pagecount
If Page="" or Clng(Page)<1 Then Page=1
If Clng(Page) > Pgnum Then Page=Pgnum
If Pgnum>0 Then Rs.AbsolutePage=Page
If Rs.Eof Then
搜索更多相关主题的帖子: SQL语句 表中 字段 Order 
2006-11-17 23:30
gmqq
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-2
得分:0 
我这样写了但是不对Sql="Select * From MovieList where title like '%"&keyword&"%' OR(AND) content like '%"&keyword&"%' Order BY ID Desc"
问题在那里??????????
2006-11-18 01:22
gmqq
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-2
得分:0 

我试着用Sql="Select * From MovieList where title like '%"&keyword&"%' or content like '%"&keyword&"%'"

获得成功,但搜索不到关健字内容或有“空格”时就会报错如下:

错误类型:
Microsoft JET Database Engine (0x80040E14)
内存溢出
/vod/so/go.asp, 第 70 行


浏览器类型:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)

网页:
POST 87 ??? /vod/so/go.asp

POST Data:
Times=1&keyword=%C7%EB%CA%E4%C8%EB%B9%D8%BC%FC%D7%D6%21&%CC%E1%BD%BB=%B8%F8%CE%D2%CB%D1



相关问题:怎样让搜索时,让一句话中只是关健变红,其它字不变红?

源码A:Response.Write "<td width=""145"" height=""55"">&nbsp;<a href=""../Html/"&Rs("ID")&".html"" target=""_blank""><font color=red>"&Rs("Title")&"</font> </a></td>"
结果出错:出现整个标题都变红了,

源码B:Response.Write "<td width=""145"" height=""55"">&nbsp;<a href=""../Html/"&Rs("ID")&".html"" target=""_blank"">"(&Rs("Title")&" ,""&keyword&"","<font color=red>"&keyword&"</font>")</a></td>"

结果出错:错误类型:
Microsoft VBScript 编译器错误 (0x800A03EA)
语法错误
/vod/so/go.asp, line 97, column 112
Response.Write "<td width=""145"" height=""55"">&nbsp;<a href=""../Html/"&Rs("ID")&".html"" target=""_blank"">"(&Rs("Title")&" ,""&keyword&"","<font color=red>"&keyword&"</font>")</a></td>"


请大家邦我看看,错在那里,感谢

[此贴子已经被作者于2006-11-18 2:31:26编辑过]

2006-11-18 01:52
gmqq
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-2
得分:0 
感谢楼上的朋友,第二个问题中的字符串被我忽略所以出错,在你的指导下我重写语句后成功,

但是第一个问题:Sql="Select * From MovieList where title like '%"&trim(keyword)&"%' or content like '%"&trim(keyword)&"%'"
这条语句只可以把空格识别,但当搜索不到关健字的时候,还是会报错,如下:
错误类型:
Microsoft JET Database Engine (0x80040E14)
内存溢出
/vod/so/go.asp, 第 69 行

我试着只查询一个表里的一个字段,代码如下:Sql="Select * From MovieList where title like '%"&keyword&"%' Order BY ID Desc"

这样一切都是正常的,但是同一个表下的content字段却不可以被查询,如果我这样写:
Sql="Select * From MovieList where title like '%"&trim(keyword)&"%' or content like '%"&trim(keyword)&"%'Order BY ID Desc"
查询时就出现:错误类型:
Microsoft JET Database Engine (0x80040E14)
内存溢出
/vod/so/go.asp, 第 69 行

看来我的SQL语句还是有问题,还得请我看一下啊,最好修正后的SQL语句,可以实现“关健字”+“关健字”的查询方式,麻烦了,谢谢

2006-11-18 13:10
gmqq
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-2
得分:0 
这样会无法搜索:
错误类型:
Microsoft VBScript 编译器错误 (0x800A0409)
未结束的字符串常量
/vod/so/go.asp, line 65, column 125
Sql="Select * From MovieList where (title like '%"&trim(keyword)&"%') or (content like '%"&trim(keyword)&"%')Order BY ID Desc

我在后面加上 " 进行语句结束后:
错误类型:
Microsoft JET Database Engine (0x80040E14)
内存溢出
/vod/so/go.asp, 第 69 行

看来我只有把整个贴出来了,如下:
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
-->
</style><!--#include File="Conn.asp" -->
<!--#include file="1.asp" -->
<%
If Session("AdminName")="0" and Session("Purview")="0" Then
Response.Redirect "Error.asp?id=005"
Else
Response.Write "<Html>"
Response.Write "<Head>"
Response.Write "<Title>移动影院 - 影视搜索 </title>"
Response.Write "<Meta Http-Equiv=""Content-Type"" Content=""Text/Html; CharSet=Gb2312"">"



Response.Write "</Head>"
Response.Write "<body topmargin=""2"">"
Response.Write "<div align=""center"">"
Response.Write "<center>"
Response.Write "<table border=""0"" cellpadding=""0"" cellspacing=""0"" bgcolor=""#CCCCCC"" style=""border-collapse: collapse"" bordercolor=""#111111"">"
Response.Write "<tr align=""center"" bgcolor=""#EEEEEE"">"
Response.Write "<td height=""25"" colspan=""3"" width=""100%""><p align=""center""><font color=""#FFFFFF"">&nbsp;"

Response.Write "</tr>"
Response.Write "<tr>"
Response.Write "<td height=""24"" bgcolor=""#EEEEEE"" width=""583""><p align=""center"">&nbsp;"
ClassID=Request("ClassID")
keyword=Request("keyword")
Set RsClass= Server.CreateObject("ADODB.Recordset")
RsClass.open "Select * From Class",Conn,1
If RsClass.Eof Then
Response.Write "暂时没有栏目"
Else

do while not RsClass.Eof


RsClass.Movenext
Loop
End If
RsClass.Close
Response.Write "</td>"
Response.Write "</tr>"
Response.Write "<tr align=""center"" valign=""top"" bgcolor=""#FFFFFF"">"
Response.Write "<td height=""293"" colspan=""3"" width=""100%"">"
Response.Write "<table border=""0"" width=""100%"" cellspacing=""0"" cellpadding=""0"" align=""center"">"
Response.Write "<form action=""go.asp"" method=""post"">"
Response.Write "<br><tr>"
Response.Write "<td width=""100%"" align=""center"">"
Response.Write "<input type=""text"" name=""keyword"" size=""50"" style=""font-size: 12px; border-left-width: 1; border-right-style: solid; border-right-width: 1; border-top-width: 1; border-bottom-style: solid; border-bottom-width: 1"">"
Response.Write "&nbsp;<input type=""submit"" value=""移动搜索"">"
Response.Write "</form>"
Response.Write "<table width=""100%"" border=""1"" cellpadding=""0"" cellspacing=""0"" bordercolor=""#CCCCCC"" style=""border-collapse: collapse"">"


Dim Page
Page=Request("Page")
PageSize = 15
Dim Rs,Sql
If ClassID="" Then
Sql="Select * From MovieList where (title like '%"&trim(keyword)&"%') or (content like '%"&trim(keyword)&"%') Order BY ID Desc"
Else
End if
Set Rs=Server.CreateObject("ADODB.Recordset")
Rs.open Sql,Conn,3,3
Rs.PageSize = PageSize
Totalfilm=Rs.RecordCount
Pgnum=Rs.Pagecount
If Page="" or Clng(Page)<1 Then Page=1
If Clng(Page) > Pgnum Then Page=Pgnum
If Pgnum>0 Then Rs.AbsolutePage=Page
If Rs.Eof Then
Response.Write " </a><a href=""http://www.139com.cn""><Font Color=#FF0000> 提示 : 无搜索结果,请点击这里提交想要查询的影视关健字!搜索引擎会在你提交后自动查找收录! >>>点击返回移动主页!</a>"
Else
Response.Write "<tr>"
Response.Write "<td width=""100"" height=""25"" align=""center"" bgcolor=""dddddd"">图片预览</td>"
Response.Write "<td width=""150"" height=""25"" align=""center"" bgcolor=""dddddd"">电影名称</td>"
Response.Write "<td width=""230"" height=""25"" align=""center"" bgcolor=""dddddd"">简介</td>"
Response.Write "<td width=""80"" height=""25"" align=""center"" bgcolor=""dddddd"">类别</td>"
Response.Write "<td width=""50"" height=""25"" align=""center"" bgcolor=""dddddd"">点播次数</td>"
Response.Write "<td width=""160"" height=""25"" align=""center"" bgcolor=""eeeeee"">主演</td>"

Response.Write "</tr>"
Count=0
Do While Not (Rs.Eof Or Rs.Bof) And Count<Rs.PageSize
Response.Write "<tr>"
Response.Write "<td width=90 height=60 align=center >"
Response.Write("<a target=_blank href=""../Html/"&Rs("ID")&".html"" target=""_blank"">")
Response.Write("<img width=90 height=60 src="&rs("pic")&">")
Response.Write("</a><br>")


Response.Write "<td width='145' height='55'>&nbsp;<a href='../Html/"&Rs("ID")&".html' target='_blank'>"&Replace(rs("title"),trim(keyword),"<font color='red'>"&trim(keyword)&"</font>")&" </a></td>"

Response.Write "<td width=300 height=55 align=center >"
if len(rs("content"))>60 Then
response.write (mid(rs("content"),1,60))
response.write (".....")
else
response.write (rs("content"))
end if
response.write "</a></td>"


Response.Write "<td width=""80"" height=""25"" align=""center"">"&Rs("CLASSID")&" </a></td>"
Response.Write "<td width=""50"" height=""25"" align=""center""><Font Color=#FF0000>"&Rs("hits")&" </a></td>"
Response.Write "<td width=""160"" height=""70"" align=""name""strlen=""40"">"&Rs("name")&"</a></td>"

Response.Write "</tr>"
Rs.Movenext
Count=Count+1
Loop
End If
Response.Write "</table>"
Response.Write "</td>"
Response.Write "</tr>"
Response.Write "<tr bgcolor=""#EEEEEE"">"
Response.Write "<td height=""25"" colspan=""3"" align=""center"" width=""100%"">"
Response.Write "<Form Method=""Post"" action=""go.asp"">"
Response.Write "[第<Font Color=#FF0000>"& Page &"</font>页/共"& Rs.PageCount &"页] "
If Page=1 Then
Response.Write "[首 页] [上一页] "
Else
Response.Write "[<a href=""go.asp?Page=1&ClassID="& ClassID &""">首 页</a>] "
Response.Write "[<a href=""go.asp?Page="& Page-1 &"&ClassID="& ClassID &""">上一页</a>] "
End If
If Rs.PageCount-Page<1 Then
Response.Write "[下一页] [尾 页]"
Else
Response.Write "[<a href=""go.asp?Page="& Page+1 &"&ClassID="& ClassID &""">下一页</a>] "
Response.Write "[<a href=""go.asp?Page="& Rs.PageCount &"&ClassID="& ClassID &""">尾 页</a>]"
End If
Response.Write " 共[<Font Color=#FF0000>"& Totalfilm &"</font>]部电影"
Response.Write " 转到:<Input Type='Text' Name=""Page"" Size=2 Maxlength=10 value="""& Page &""" align=""center"">"
Response.Write "<Input Type=""Submit"" value=""Goto"" Size=2 Name=""Submit"">"
Response.Write "<Input Type=""Hidden"" value="""& ClassID &""" Name=""Hidden"">"
Rs.Close
Response.Write "</td>"
Response.Write "</tr>"
Response.Write "<tr>"
Response.Write "<td height=""25"" align=""center"" bgcolor=""#EEEEEE"" colspan=""3""><font color=""#FFFFFF"">&copy;"

Response.Write "</tr>"
Response.Write "</table>"
Response.Write "</center>"
Response.Write "</div>"
Response.Write "</body>"
Response.Write "</html>"
End If %>



2006-11-18 13:33
gmqq
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-2
得分:0 
错误类型:
Microsoft VBScript 编译器错误 (0x800A0409)
未结束的字符串常量
/vod/so/go.asp, line 65, column 126
Sql="Select * From MovieList where (title like '%"&trim(keyword)&"%') or (content like '%"&trim(keyword)&"%') Order BY ID Desc

我增加"完结语句后:Sql="Select * From MovieList where (title like '%"&trim(keyword)&"%') or (content like '%"&trim(keyword)&"%') Order BY ID Desc"

错误类型:
Microsoft JET Database Engine (0x80040E14)
内存溢出
/vod/so/go.asp, 第 69 行

这条语句还是不对????


相关源码:Dim Page
Page=Request("Page")
PageSize = 15
Dim Rs,Sql
If ClassID="" Then
Sql="Select * From MovieList where title like '%"&trim(keyword)&"%' or content like '%"&trim(keyword)&"%'"
Else
End if
Set Rs=Server.CreateObject("ADODB.Recordset")
Rs.open Sql,Conn,3,3
Rs.PageSize = PageSize
Totalfilm=Rs.RecordCount
Pgnum=Rs.Pagecount
If Page="" or Clng(Page)<1 Then Page=1
If Clng(Page) > Pgnum Then Page=Pgnum
If Pgnum>0 Then Rs.AbsolutePage=Page
If Rs.Eof Then
Response.Write " </a> 提示 : 无搜索结果,请点击这里提交想要查询的关健字!搜索引擎会在你提交后自动查找收录! >>>点击返回移动主页!</a>"
Else

在搜索不到关健字的时候,我调用最后一句进行报错提示,麻烦邦我看一下,是不是后面的语句引起的

[此贴子已经被作者于2006-11-18 13:43:55编辑过]

2006-11-18 13:37
gmqq
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-2
得分:0 
要是方便请加我QQ:393195095万分感谢  做人很低调
2006-11-18 13:47
gmqq
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-2
得分:0 
文本
2006-11-18 13:56
gmqq
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-2
得分:0 
问题结贴:

在做人很低调朋友的帮助下,最后发现问题出现在:日文字符:上,第4张贴子提示为不可以有日文字符,是对的,本有由衷感谢做人很低调朋友的帮助,相亲问题解决如下:

26个日文片假名会导致Access搜索(80040e14/内存溢出)
ゴ ガ ギ グ ゲ ザ ジ ズ ヅ デ ド ポ ベ プ ビ パ ヴ ボ ペ ブ ピ バ ヂ ダ ゾ ゼ
解决办法:
当字段内包含了这26个日文字符任意一个多个时,就会导致在执行SQL语句中包含了
[字段] like '%aaaaa%' 或 inStr(1,[字段],'aaaaa',1)>0
这样的查询时,毫无道理的出现了
"Microsoft JET Database Engine 错误 '80040e14' 内存溢出"的错误
其他Jet SQL函数命令未作测试,大概与字符搜索定位匹配相关的都可能出错
搜索相关资料得知被微软工程师证实是Access的bug,可能是语法关系都是微软的东东
在vbs中 执行inStr(1,日文平假名变量,"aaaaa",1)依然要出现错误
Microsoft VBScript 运行时错误 错误 '800a0005' 无效的过程调用或参数: 'instr'
没有搜索,因这几个字符出现Access的论坛网站搜索无法进行,何等痛苦
昨天一朋友大叫怪事,他的音乐数据库无法搜索了,只有30000条记录时是好的
毫无疑问,日文片假名是祸根,花几分钟把有包含上面的日文替换成"?"搜索顺利恢复
找来论坛程序用户群最大的动网dvBBS AC版本 7.0SP2 版测试,同样有这个日文发帖后 导致无法搜索并且运行时出错的问题
线上去搜索 '80040e14' 内存溢出" 的错误 多的是!
一简单有效的解决办法:
对这26个字符进行编码和解码,可能效率感觉不理想,测试下来问题不大,速度影响不是太大
编码:
Function Jencode(byVal iStr)
if isnull(iStr) or isEmpty(iStr) then
Jencode=""
Exit function
end if
dim F,i,E
' F=array("ゴ","ガ","ギ","グ","ゲ","ザ","ジ","ズ","ヅ","デ",_
' "ド","ポ","ベ","プ","ビ","パ","ヴ","ボ","ペ","ブ","ピ","バ",_
' "ヂ","ダ","ゾ","ゼ")
E=array("Jn0;","Jn1;","Jn2;","Jn3;","Jn4;","Jn5;","Jn6;","Jn7;","Jn8;","Jn9;","Jn10;","Jn11;","Jn12;","Jn13;","Jn14;","Jn15;","Jn16;","Jn17;","Jn18;","Jn19;","Jn20;","Jn21;","Jn22;","Jn23;","Jn24;","Jn25;")
F=array(chr(-23116),chr(-23124),chr(-23122),chr(-23120),_
chr(-23118),chr(-23114),chr(-23112),chr(-23110),_
chr(-23099),chr(-23097),chr(-23095),chr(-23075),_
chr(-23079),chr(-23081),chr(-23085),chr(-23087),_
chr(-23052),chr(-23076),chr(-23078),chr(-23082),_
chr(-23084),chr(-23088),chr(-23102),chr(-23104),_
chr(-23106),chr(-23108))
Jencode=iStr
for i=0 to 25
Jencode=replace(Jencode,F(i),E(i))
next
End Function
解码:
Function Juncode(byVal iStr)
if isnull(iStr) or isEmpty(iStr) then
Juncode=""
Exit function
end if
dim F,i,E
' F=array("ゴ","ガ","ギ","グ","ゲ","ザ","ジ","ズ","ヅ","デ",_
' "ド","ポ","ベ","プ","ビ","パ","ヴ","ボ","ペ","ブ","ピ","バ",_
' "ヂ","ダ","ゾ","ゼ")
E=array("Jn0;","Jn1;","Jn2;","Jn3;","Jn4;","Jn5;","Jn6;","Jn7;","Jn8;","Jn9;","Jn10;","Jn11;","Jn12;","Jn13;","Jn14;","Jn15;","Jn16;","Jn17;","Jn18;","Jn19;","Jn20;","Jn21;","Jn22;","Jn23;","Jn24;","Jn25;")
F=array(chr(-23116),chr(-23124),chr(-23122),chr(-23120),_
chr(-23118),chr(-23114),chr(-23112),chr(-23110),_
chr(-23099),chr(-23097),chr(-23095),chr(-23075),_
chr(-23079),chr(-23081),chr(-23085),chr(-23087),_
chr(-23052),chr(-23076),chr(-23078),chr(-23082),_
chr(-23084),chr(-23088),chr(-23102),chr(-23104),_
chr(-23106),chr(-23108))
Juncode=iStr
for i=0 to 25
Juncode=replace(Juncode,E(i),F(i))'□
next
End Function
注意,如果直接使用字符不方便(windows还没装日文支持),注释掉的部分提供有 chr(-23804) ..这样的定义
这样
1.
表单输入保存时,使用Jencode()将这26个字符先编码再保存(为什么是这26个字符,经过全部测试87个平假名89个片假名最终认定的)

ゴ 即 chr(-23116) 编码为 Jn1;
2.
显示时,则使用 Juncode() 函数进行解码,还原日文片假名显示
3.
搜索关键字,也要使用 Jencode() 进行编码后再放入 like里
where [Topic] like '%Jencode(kewwords)%' 使用
才能保证搜索的值和编码过的数据库字段内容匹配
==================================
PS:
也可以使用正则表达式来改写上面的两个函数,或许效率还要更高些
再就是如果 压根不使用日文,也不需要搜索日文,则解码部分可以不用,保存数据实直接把这26个片假名字符替换为空字符或任一字符,比如"□"
抛砖引玉,如果有更本质的真正的好方法,谢分享
附:
----------------------------
平假名87个 asc值
-23391 --> -23316
unicode 3040-309F
ぁあぃいぅうぇえぉお
かがきぎくぐけげこご
さざしじすずせぜそぞ
ただちぢっつづてでと
どなにぬねのはばぱひ
びぴふぶぷへべぺほぼ
ぽまみむめもゃやゅゆ
ょよらりるれろゎわゐ
ゑをん゛゜ゝゞ
------------------------------
片假名89个 asc值
-23135 -> -23059
unicode 30A0-30FF
ァアィイゥウェエォオ
カガキギクグケゲコゴ
サザシジスズセゼソゾ
タダチヂッツヅテデト
ドナニヌネノハバパヒ
ビピフブプヘベペホボ
ポマミムメモャヤュユ
ョヨラリルレロヮワヰ
ヱヲンヴヵヶーヽヾ
---------------------------------------------------------------------
另一网友发的错误情况:
ACCESS数据库含有某些特定日文字符时,like检索可能会出错的几种情况:
连接方式与打开方式
========================
conn.open "driver={microsoft access driver (*.mdb)};dbq=" & DatabaseToQuery
rs_str.open StrSQL,conn,1,1
出错
========================
错误描述: ODBC 驱动程序不支持所需的属性。
错误编号: -2147217887
错误类型:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E21)
/syl/access-syl-str.asp, 第 442 行
连接方式与打开方式
========================
conn.open "driver={microsoft access driver (*.mdb)};dbq=" & DatabaseToQuery
rs_str.open StrSQL,conn,1,3
出错
========================
错误描述: [Microsoft][ODBC Microsoft Access Driver] 内存溢出
错误编号: -2147217913
错误类型:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E07)
连接方式与打开方式
========================
conn.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DatabaseToQuery
rs_str.open StrSQL,conn,1,1
出错
========================
错误描述: 内存溢出
错误编号: -2147217900
错误类型:
Microsoft JET Database Engine (0x80040E14)
连接方式与打开方式
========================
conn.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DatabaseToQuery
rs_str.open StrSQL,conn,1,3
出错
========================
错误描述: 内存溢出
错误编号: -2147217900
错误类型:
Microsoft JET Database Engine (0x80040E14)
解决办法就是按照“做人很低调”朋友的办法去除数据库中的日文字符。


在次由衷感谢做人很低调朋友的帮助!

[此贴子已经被作者于2006-11-18 17:17:35编辑过]

2006-11-18 17:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-103732-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.326769 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved