Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pulling Results from Two Fields

Status
Not open for further replies.

KellyHero

Programmer
Jun 10, 2002
14
US
Hi all:

I need some assistance please. I'm developing an ASP search page for a website and I'm having problems writing an SQL query statement that returns the results accurately.

Here is what I currently have:

Code:
MSQL = "SELECT * FROM properties WHERE act = 'Active' "

If Not Request.Querystring("s") = "" Then
qs = Split(Request.Querystring("s"), " ")
For i = 0 to UBound(qs)
 MSQL = MSQL & "AND pr_desc LIKE '%" & qs(i) & "%' "
Next
End If

If Not Request.Querystring("ag") = "" And Not Request.Querystring("ag") = "all" Then
ag = Request.Querystring("ag")
MSQL = MSQL & "AND pr_agent = " & ag & "  OR pr_agent2 = " & ag & " "
End If
If you search for a property only by keyword ("s") the results returned are accurate. If you search for a property only by agent1 or agent2, the results are accurate.

If you search for a property by keyword AND by agent, the results show properties which have either the keyword OR the agent is in agent2 field.

How can I combine the querystring so that it pulls only results that include the keyword AND the agent, whether the agent is agent1 or agent2?
 
Have not built an ASP SQL string before but the logic seems to be obvious so I will give it a try:

MSQL = "SELECT * FROM properties WHERE act = 'Active' "

If Not Request.Querystring("s") = "" Then
qs = Split(Request.Querystring("s"), " ")
For i = 0 to UBound(qs)
MSQL = MSQL & "AND pr_desc LIKE '%" & qs(i) & "%' "
Next
End If

If Not Request.Querystring("ag") = "" And Not Request.Querystring("ag") = "all" Then
ag = Request.Querystring("ag")
MSQL = MSQL & "AND (pr_agent = " & ag & " OR pr_agent2 = " & ag & " )"
End If

I included the red parens to group the agent expressions together in that it is an or expression. If either agent is selected it matches with the AND for the rest of the selection.

Post back with the results.


Bob Scriver
Want the best answers? See FAQ181-2886
Nobody believes the official spokesman... but everybody trusts an unidentified source.
Author, Bagdad Bob???

 
Hey great. That was a little bit of a long shot but SQL is SQL with a little twist. You always have to be careful when you mis AND's and OR's.

Good luck with your project.

Bob Scriver
Want the best answers? See FAQ181-2886
Nobody believes the official spokesman... but everybody trusts an unidentified source.
Author, Bagdad Bob???

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top