SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru johnjohn2's Avatar
    Join Date
    Apr 2004
    Location
    here
    Posts
    746
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sql question from a nOOb

    I've managed to get the following code to do 99% of what I want it to:

    SELECT tbl3.col1, tbl3.col11, tbl3.col2, tbl3.col3, tbl3.col4, tbl3.col5, tbl3.col6, tbl3.col7, tbl3.col8, tbl3.col13
    FROM tbl3
    WHERE ((tbl3.col13 = 1) AND ((tbl3.col11 = colcheck) AND (tbl3.col2 LIKE '%colname%' AND tbl3.col6 LIKE '%colcity%' AND tbl3.col7 LIKE '%colstate%' AND tbl3.col4 LIKE '%coladdress%' AND tbl3.col8 LIKE '%colzip%')))
    ORDER BY tbl3.col2 ASC

    It for an advanced search form. col13 is set manually in the code, col11 is a radio button, and the rest of the fields are optional. It's for finding contacts in a table. I have colname which is the company name, but also a want to check it against col3 (not only col2). Same with coladdress, I'd like to check it against col5 (not only col4). Does anyone know how to accomplish this?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,021
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    do yourself a favour, get rid of those unnecessary parentheses, so that the ones you actually need will stand out more
    Code:
    select tbl3.col1
         , tbl3.col11
         , tbl3.col2
         , tbl3.col3
         , tbl3.col4
         , tbl3.col5
         , tbl3.col6
         , tbl3.col7
         , tbl3.col8
         , tbl3.col13
      from tbl3
     where tbl3.col13 = 1 
       and tbl3.col11 = colcheck 
       and ( 
           tbl3.col2 like '%colname%' 
        or tbl3.col3 like '%colname%' 
           )
       and tbl3.col6 like '%colcity%' 
       and tbl3.col7 like '%colstate%' 
       and (
           tbl3.col4 like '%coladdress%' 
        or tbl3.col5 like '%coladdress%' 
           )
       and tbl3.col8 like '%colzip%'
    order 
        by tbl3.col2 asc
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Guru johnjohn2's Avatar
    Join Date
    Apr 2004
    Location
    here
    Posts
    746
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I gave your code a try and it worked perfectly. But I tried adding a few fields on the end of it and it doesn't work at all now. Everything looks ok to me, not sure what's wrong. Does the order matter at all?

    SELECT tbl3.col1, tbl3.col11, tbl3.col2, tbl3.col3, tbl3.col4, tbl3.col5, tbl3.col6, tbl3.col7, tbl3.col8, tbl3.col13, tbl3.col17, tbl3.col18, tbl3.col19
    FROM tbl3
    WHERE tbl3.col13 = 1 and tbl3.col11 = colcheck and (tbl3.col2 like '%colname%' or tbl3.col3 like '%colname%') and tbl3.col6 like '%colcity%' and tbl3.col7 like '%colstate%' and (tbl3.col4 like '%coladdress%' or tbl3.col5 like '%coladdress%') and tbl3.col8 like '%colzip%' and tbl3.col17 like '%colrisk%' and tbl3.col18 like '%colfedid%' and tbl3.col19 like '%colclientid%'
    ORDER BY tbl3.col2 ASC

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,021
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    everything looks okay to me too

    this means it's gotta be your data

    you must be running the query with a combination of parameters for which there just aren't any rows
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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