SQL Question

Results 1 to 3 of 3

Thread: SQL Question

  1. #1
    Charles Brown Guest

    Default SQL Question

    Okay...I have a search page that let&#039s users search by keyword in 3 columns in a table using LIKE and one column where a Status has to be = to 2. However, for some reason it ignores the last criteria. Here is my SQL statement:<BR><BR>SELECT * FROM MyTable WHERE MyTable.[Column1] Like &#039%" & (Request.Form("SearchCriteria")) & "%&#039 OR MyTable.[Column2] Like &#039%" & (Request.Form("SearchCriteria")) & "%&#039 OR MyTable.[Column3] Like &#039%" & (Request.Form("SearchCriteria")) & "%&#039 AND MyTable.[Status]=2<BR><BR>For some reason, it does the keyword search fine. However, it still pulls up records where the Status=1, which it shouldn&#039t. Any ideas?

  2. #2
    Join Date
    Dec 1969

    Default RE: SQL Question

    The problem is your use of OR and AND. Right now you are saying:<BR><BR>if column1 = whatever OR column2 = whatever OR column3 = whatever OR column4 = whatever AND column5 = 2<BR><BR>This will return any column1 that matches whatever, any column2 that matches whatever, any column3 that matches whatever, and any column4 that matches whatever and has column5 = 2. Instead, you need to do:<BR><BR>if column1 = whatever AND column5 = 2 OR column2 = whatever AND column5 = 2...etc.<BR><BR>HTH<BR>Chris

  3. #3
    Charles Brown Guest

    Default RE: SQL Question

    Thank you, thank you, thank you. That was so obvious that I looked right over it. Thanks again!!! The app is working great now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts