SQL query for search method

Discussion in 'Programming' started by f5mtadas, Jun 14, 2012.

  1. #1
    Hi there,

    I need help to write sql query for my search form, but I don't know how to write the sql command
    that can be used to display automatically by one, two or three criterias.

    Here is the form:

    Article_Search_Form.jpg

    IT should work like that:

    If I insert data in first field on the top of form it should show all date by that inserted data,
    but if I insert data in another form field it should filter by first field and by second field.

    If I insert data into first three fields it should show all data by all these three fields, but
    if I insert data into only third field it should show the list only for that third field data.

    HOW TO WRITE SQL COMMAND?

    Thanks
     
    f5mtadas, Jun 14, 2012 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    Since we OBVIOUSLY know nothing about your existing database this question can't be easily answered without assumption...

    You could use something similar to:

    SELECT some_col1, some_col2, some_col3 FROM Some_Table WHERE some_col1 = 'something' OR some_col_2 = 'something';

    You could use LIKE 'blah%' to search non-exact matches.

    SELECT some_col1, some_col2, some_col3 FROM Some_Table WHERE some_col1 LIKE 'something%' OR some_col_2 LIKE 'something%';
     
    NetStar, Jun 15, 2012 IP
  3. f5mtadas

    f5mtadas Member

    Messages:
    308
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Thank you, it really does not matter what database collumn is I need just the principle. I am programing on C# and already created a method.
     
    f5mtadas, Jun 15, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    It's not one SQL statement. Each time the user enters another field, you create a new SQL statement that uses all the fields entered so far. Then you fill in whatever gets filled in using the data returned by the query.
     
    Rukbat, Jun 16, 2012 IP
  5. Ev0Lv

    Ev0Lv Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    Yea, it is pretty simple if you have the syntax and the principle of each query. Glad you find your way . :)
     
    Ev0Lv, Jun 17, 2012 IP