How to search from multiple tables in mysql. (PHP)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meisjen86
    New Member
    • Sep 2011
    • 1

    How to search from multiple tables in mysql. (PHP)

    Hey guys,

    I'm working on a project from school. I want to be able to search for a term across multiple tables in a database. For ex. I have two tables(table a, table b) set up the exact same way, when someone searches for the term on the search box, I want the results to come from both tables(table a, table b).

    Here is a snippet of my code. This only shows that it's searching one table within the database which is 'articles' , anyone know a way I can search both articles and articles 2 (my second table)?

    BTW I looked at join, which seems to be for information that is different but goes together, I think I need union but honestly not sure how to get the syntax right.

    Code:
      $results = "SELECT `title`, LEFT(`description`, 70) as `description`, `url` FROM `articles` WHERE $where";
      $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;
  • John Doe
    New Member
    • Jun 2011
    • 20

    #2
    Meisjen,

    the mySQL statement you're looking for is UNION ...

    select * from table1 UNION select * from table 2

    Comment

    Working...