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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql select problem

Status
Not open for further replies.

aamaker

Programmer
Jan 20, 2002
222
US
Im trying to search a database giving the user the ability to filter based on 3 different criteria

- a text field
- list menu criteria pulled from db
- another list menu criteria pulled from db...

my problem is the search works in some cases but in others just returns ALL the records...

heres the sql select code Im using:

Code:
SELECT *
FROM tbl_events
WHERE EventTitle Like '%var1%' Or EventDetail Like  '%var1%' And City = 'var2' And EventCategory Like '%var3%'
 
The problem here is that you are mixing AND with OR in the where clause. Since I can never remember which takes precedence, I always use parenthesis when mixing and with or.

Ex:

Code:
WHERE [!]([/!]EventTitle Like '%var1%' 
         Or EventDetail Like  '%var1%'[!])[/!]
      And City = 'var2' 
      And EventCategory Like '%var3%'

This still may not give you the expected results, but it should get you a little closer.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Works like a charm now, thanks much!

 
Hi,
Also be sure to force var1 and var3 to have some value else all records matching var2's value could be returned since an empty string in either var1 or var3 will create a where clause that is just wild cards for those fields..so any record would match.



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top