Trouble with query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • edwinparker
    New Member
    • Mar 2007
    • 3

    Trouble with query

    I'm wondering if someone might be able to help me with a query in mysql.

    I have 3 tables that contain the information I need.

    Table 1
    Id1
    .
    .
    .

    Table 2
    Id2
    Id1(FK)
    .
    .
    .

    Table 3
    Id3
    Id2(FK)
    .
    .
    .

    I want to pull all the information from these tables if table 2 has more than one of the same Id1(FK) [relation to Table1]

    So far I have:
    SELECT * FROM table2 INNER JOIN table1 ON (table2.Id1 = table1.Id1)
    INNER JOIN table3 ON (table2.Id2 = table3.Id2)
    GROUP BY table1.Id1
    HAVING COUNT(table1.Id 1) > 1

    This works fine if I have data in table 3, but the relation is 0-many for table 2 & 3, so I might not have anything in table 3 relating to table 2 and if that's the case I don't get any results. Do I need to use some kind of left or right outer join? I've messed around with these too with no luck. I'm not as good at SQL as I'd like to be so I'm kind of stuck as to what to try now. Any help would be appreciated. Please let me know if more information is required and I can post the acutal tables.
  • ScarletPimpernal
    New Member
    • Mar 2007
    • 39

    #2
    Hi,

    May be this query will help you,

    SELECT * FROM table2 INNER JOIN table1 ON (table2.Id1 = table1.Id1)
    LEFT OUTER JOIN table3 ON (table2.Id2 = table3.Id2)
    GROUP BY table1.Id1
    HAVING COUNT(table1.Id 1) > 1;

    Thanks,
    Scarlet


    Originally posted by edwinparker
    I'm wondering if someone might be able to help me with a query in mysql.

    I have 3 tables that contain the information I need.

    Table 1
    Id1
    .
    .
    .

    Table 2
    Id2
    Id1(FK)
    .
    .
    .

    Table 3
    Id3
    Id2(FK)
    .
    .
    .

    I want to pull all the information from these tables if table 2 has more than one of the same Id1(FK) [relation to Table1]

    So far I have:
    SELECT * FROM table2 INNER JOIN table1 ON (table2.Id1 = table1.Id1)
    INNER JOIN table3 ON (table2.Id2 = table3.Id2)
    GROUP BY table1.Id1
    HAVING COUNT(table1.Id 1) > 1

    This works fine if I have data in table 3, but the relation is 0-many for table 2 & 3, so I might not have anything in table 3 relating to table 2 and if that's the case I don't get any results. Do I need to use some kind of left or right outer join? I've messed around with these too with no luck. I'm not as good at SQL as I'd like to be so I'm kind of stuck as to what to try now. Any help would be appreciated. Please let me know if more information is required and I can post the acutal tables.

    Comment

    Working...