search data in 2 tables

Discussion in 'PHP' started by roice, Mar 12, 2011.

  1. #1
    Hello,

    I have 2 tables of books:
    1. books
    2. books_archive

    how do I write query that search "title" in both tables?
    WHERE (table = '$title')
    in both table...

    ?

    Thank you in advance.
     
    roice, Mar 12, 2011 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    you can select from more than 1 table, using the as method. e.g.:

    SELECT * FROM `books` as T1, `books_archive` as T2 WHERE `name` = :name
     
    ssmm987, Mar 12, 2011 IP
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    and than the result will be from both tables?
     
    roice, Mar 12, 2011 IP
  4. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    and one more thing - is it possible to ask in which table the result has found?
     
    roice, Mar 12, 2011 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    If both tables will have the same field names, you can use UNION and add a table_name field.
     
    ThePHPMaster, Mar 12, 2011 IP