|
SQL OR Search Example by Charles
Carroll
AND and OR operators expand the power of the WHERE
clause and provide a powerful tool to check multiple conditions. The Basic
Guidelines are as follows:
If your goal is that several if conditions must ALL BE TRUE
to suceed, this is the Role of the AND within a WHERE clause, i.e.
"select * from publishers where state='MD' and city='Rockville'
"select * from authors where Year_Born>1960 and Year_Born<1970'
If several conditions can indivually be true this is the Role
of an OR within a WHERE clause, i.e.
"select * from publishers where state='MD' OR state='NY'
filename=/learn/test/SQLcities.asp
<HEAD><TITLE>SQLcities.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<Form action = "SQLcitiesrespond.asp" method="POST">
Choose City (or Cities):<p>
<%
call query2listm("select distinct city from publishers", _
"cy","DSN=student;uid=student;pwd=magic")
%>
<P>
<Input type="submit" value="Get Data"> <Input type="reset" value="Clear City"></form>
</BODY></HTML>
<!--#include virtual="/learn/test/lib_dblistm.asp"-->
The responder looks like this:
filename=/learn/test/SQLcitiesrespond.asp
<HEAD><TITLE>sqlcitiesrespond.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
citycount=request.form("cy").count
If citycount=0 then%>
<B>You never choose a city!</b><br>
<a href="sqlcities.asp">Choose City</a>
<%
response.end
end if
firstcity=request.form("cy")(1)
SQLtemp="select * from publishers "
SQLtemp = SQLtemp & " where city='" & firstcity & "'"
for counter=2 to citycount
whichcity=request.form("cy")(counter)
SQLtemp = SQLtemp & " or city='" & whichcity & "' "
next
response.write SQLtemp
call query2table(SQLtemp,"DSN=student;uid=student;pwd=magic")
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>
The file lib_dbtable.asp looks like this:
filename=/learn/test/lib_dbtable.asp
<%
sub query2table(inputquery, inputDSN)
dim conntemp, rstemp
set conntemp=server.createobject("adodb.connection")
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1%>
<table border=1><tr>
<% 'Put Headings On The Table of Field Names
for i=0 to howmanyfields %>
<td><b><%=rstemp(i).name%></B></TD>
<% next %>
</tr>
<% ' Now lets grab all the records
do while not rstemp.eof %>
<tr>
<% for i = 0 to howmanyfields
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if%>
<td valign=top><%=thisvalue%></td>
<% next %>
</tr>
<%rstemp.movenext
loop%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
The file lib_dblistm.asp looks like this:
filename=/learn/test/lib_dblistm.asp
<%
SUB query2listm(myquery,myname,myDSN)
dim conntemp, rstemp
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(myquery)
%>
<Select name="<%=myname%>" multiple>
<%
do while not rstemp.eof
thisfield=trim(RStemp(0))
if isnull(thisfield) or thisfield="" then
' ignore
else
response.write "<option>" & thisfield & "</option>"
end if
rstemp.movenext
loop
%>
</select>
<%rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
END SUB
%>
|