0

I have 3 tables, all linked. Table 1, Table 2 and Table 3. 1 to many. Table 1 will always have records in Table 2. However, Table 2 does not have to have any records in Table 3.

I want to get the id's and names of the records in Table 2 for a record in Table 1 only if they have records in Table 3.

here's my query

SELECT table2.index , table2.name
FROM table2 , table3
WHERE table2.fk_table1_index` = '126'
AND table2.index = 'table3.fk_table2_id'
GROUP BY table2.index

Now...this returns no records in my php code or phpMyadmin, even though, it worked when I was testing it out in phpMyadmin.

I only want 1 result for each record in Table 2. For example, if a record in Table 2 has 3 records in Table 3, I only want the id and name to show up once in the result. That's why I have the GROUP BY part.
This seems like a simple problem, but I have no idea how to solve it.

Any ideas? Thanks in advance.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by apegram
0

You're selecting two non-aggregated fields and only grouping on one. For the query to execute properly, you need to group on each field that is not an aggregate or a function. But as for this example, consider dropping the Group By clause in favor of Select Distinct.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.