Query from multiple tables takes forever.

Discussion in 'Databases' started by mahmood, Dec 26, 2007.

  1. #1
    Is there anything wrong with this query? I tried it on my PHPMyAdmin and my own php files and it takes forever and never returns any results.
    SELECT table1.*,table2.* FROM table1,table2 WHERE 
    table1.field1='something' OR table2.field1='something'
    PHP:
    If I remove the OR part everything goes well but when I have that OR there it doesn't work.

    .
     
    mahmood, Dec 26, 2007 IP
  2. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #2
    If you are using two tables, you should also put a join condition
    based on the common field, e.g.

    
    SELECT table1.*,table2.* 
    FROM table1,table2 
    WHERE 
    (table1.field1='something' OR table2.field1='something')
    and table1.commonid = table2.commonid
    PHP:
    Also check that you have an index on these fields.
     
    Kuldeep1952, Dec 26, 2007 IP
  3. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #3
    But there is no commonid on tables and even if there was they wouldn't be the same because "something" is either on table1 or table2.
     
    mahmood, Dec 27, 2007 IP
  4. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #4
    If there is no join field, perhaps you should then use a UNION:

    SELECT table1.field1 WHERE table1.field1='something'
    UNION
    SELECT table2.field1 WHERE table2.field1='something'
    PHP:
     
    Kuldeep1952, Dec 27, 2007 IP
  5. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    to make things easier if those tables don't have common link, then create separate select statements.
     
    kendo1979, Dec 27, 2007 IP