Quick SQL Question...

Discussion in 'OT Technology' started by Sexual Vanilla, Sep 21, 2007.

  1. Sexual Vanilla

    Sexual Vanilla New Member

    Joined:
    May 23, 2005
    Messages:
    6,305
    Likes Received:
    0
    Location:
    South Carolina
    What is the quickest way to search more than one row in one particular table for one keyword? I'm not trying to search all tables...just one.

    i.e. I have 4 rows in a table I'd like to search in for one keyword.
     
  2. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    select * from <table>
    where <column1> = word OR
    where <column2> = word OR
    where <column3> = word OR
    where <column4> = word

    :dunno:
     
  3. Frequency

    Frequency New Member

    Joined:
    Dec 30, 2004
    Messages:
    7,504
    Likes Received:
    0
    Location:
    PA
    Need more details?

    To search for a keyword throughout the whole table in a column you can use LIKE
     
  4. Sexual Vanilla

    Sexual Vanilla New Member

    Joined:
    May 23, 2005
    Messages:
    6,305
    Likes Received:
    0
    Location:
    South Carolina
    Thanks for the advice...basically I have a table with a filename row, along with 4 keyword rows that describe the file. The user can search by as few as one keyword, or as many as 4. After I set up the table, I realized my mistake of having it this way. If the user created the file with "bob" in the keyword 1 row, then he wouldn't find it if he searched for "bob" in any of the other keyword rows, because it's only held in the first keyword field.
     
  5. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    You should probably merge those keyword fields into one. Whenever someone adds keywords to a file you could just have it space (or comma) delimit them: keyword1 keyword2 keyword3

    Then whenever you query based off the keywords you'd just do:

    $result = mysql_query("SELECT * FROM files WHERE keyword_field LIKE '%" . $searchQuery . "%'");
     
  6. Sexual Vanilla

    Sexual Vanilla New Member

    Joined:
    May 23, 2005
    Messages:
    6,305
    Likes Received:
    0
    Location:
    South Carolina
    I ended up using the code mentioned above along with PHP's explode function in order to achieve what I wanted. Thanks.
     

Share This Page