selecting a field from MULTIPLE tables

Discussion in 'Databases' started by Weirfire, Oct 6, 2005.

  1. #1
    I have a number of different tables all containing a field called keywords

    I want to list all the titles from each table where a certain keyword exists and I was wondering if it is possible to do this in a single query?

    Would something like

    SELECT title FROM table1, table2, table3 WHERE keywords LIKE '%$keyword%'


    work?
     
    Weirfire, Oct 6, 2005 IP
  2. DangerMouse

    DangerMouse Peon

    Messages:
    275
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can do!

    select table1.field as field1, table2.field as field2
    from table1
    inner join table2 on table1.fieldx = table2.fieldy
    where ...

    I don't think you can actually merge 2 fields together though...
     
    DangerMouse, Oct 6, 2005 IP
    Weirfire likes this.
  3. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #3
    Are the tables connect in anyway?

    If not (and even if so) you'll probably be best using union
    
    SELECT title FROM table1 WHERE keywords LIKE '%$keyword%'
    UNION
    SELECT title FROM table2 WHERE keywords LIKE '%$keyword%'
    UNION
    SELECT title FROM table3 WHERE keywords LIKE '%$keyword%'
    
    Code (markup):
     
    dct, Oct 6, 2005 IP
    Weirfire likes this.
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    Thanks guys. Thats what I needed to know! :)
     
    Weirfire, Oct 6, 2005 IP