Group by querying two tables

Discussion in 'PHP' started by lektrikpuke, Aug 3, 2009.

  1. #1
    Hi guys,

    Group by seems fairly straightforward when querying one table, but two is something I'm not having luck with. I've tried:

    $quick_query = "SELECT * FROM c_tbl c, p_tbl p WHERE c.active = 1 AND p.active = 1 AND c.id = p.id AND c.code like '" . $cat . "'";
    $quick_query .= " GROUP BY p.my_list";

    And

    $quick_query = "SELECT c.active, p.active, c.id, p.id, c.code, p.my_list FROM c_tbl c, p_tbl p WHERE c.active = 1 AND p.active = 1 AND c.id = p.id AND c.code like '" . $cat . "'";
    $quick_query .= " GROUP BY p.my_list";

    Both time out or give error messages if changed ever so slightly.

    Any help?

    Rob :D
     
    Last edited: Aug 3, 2009
    lektrikpuke, Aug 3, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Error messages would be helpful.

    Also, do an EXPLAIN on these to see why they time out. Your indices are probably not optimized.
     
    premiumscripts, Aug 3, 2009 IP
  3. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #3
    Error messages are usually when it's not sure which id it should get (or that I'm requesting). With those queries, it's just timeouts. And without the group by part, they pop right up, so I don't think it's an optimization issue.
     
    lektrikpuke, Aug 3, 2009 IP
  4. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #4
    I did an EXPLAIN and EXPLAIN EXTENDED. They both gave the following:

    +----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------+
    | 1 | SIMPLE | p | ALL | NULL | NULL | NULL | NULL | 2096 | Using where; Using temporary; Using filesort |
    | 1 | SIMPLE | c | ALL | NULL | NULL | NULL | NULL | 7315 | Using where |
    +----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------+
    2 rows in set, 1 warning (0.00 sec)

    Does this tell me anything? :eek:
     
    lektrikpuke, Aug 3, 2009 IP
  5. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #5
    If I remove the GROUP BY part of the statement, it works just fine. What gives?
     
    lektrikpuke, Aug 3, 2009 IP
  6. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #6
    It would seem you were correct. Once I added comparison indexes to both tables, the problem went away - started working is what I mean. Thank you for your help. :D
     
    lektrikpuke, Aug 3, 2009 IP