views:

32

answers:

3

A the moment I am using two queries one is called initially, and the second is called during a loop through the results of the first. I want to combine both queries, but have been unable to so far. The tables the queries are pulling from are:

+--------------+    +--------------+    +--------------------------+
|    table_1   |    |    table_2   |    |          table_3         |
+----+---------+    +----+---------+    +----+----------+----------+
| id |   name  |    | id |   name  |    | id |  tbl1_id |  tbl2_id | 
+----+---------+    +----+---------+    +----+----------+----------+
| 1  | tbl1_1  |    | 1  | tbl2_1  |    | id |     1    |     1    |
| 2  | tbl1_2  |    | 2  | tbl2_2  |    | id |     3    |     2    |
| 3  | tbl1_3  |    | 3  | tbl2_3  |    | id |     3    |     3    |
| 4  | tbl1_4  |    +----+---------+    +----+----------+----------+
+----+---------+

There is a many to many relationship between table_1 and table_2 in table_3. I have been using to separate queries so far. One query to return all the contents of table_1 and a second query to return the values of table_2 that are connected to table_1 through table_3. However, I would like to do away with the loop and lessen the amount of queries being sent to the server. I have tried using a JOIN:

SELECT table_1.id, table_1.name, table_2.id, table_2.name
FROM table_3
LEFT JOIN table_1 ON (table_3.tbl1_id = table_1.id)
LEFT JOIN table_1 ON (table_2.tbl2_id = table_2.id)

This returned pretty much want I wanted except it only returned the values that were in table_3 leaving out some of the values from table_1. I have tried using subqueries:

SELECT  table_1.id,
    table_1.name,
    (SELECT table_2.id FROM table_2, table_3 WHERE table_2.id = table_3.tbl2_id AND table_1.id = table_3.tbl1_id) AS tbl_2_id,
    (SELECT table_2.name FROM table_2, table_3 WHERE table_2.id = table_3.tbl2_id AND table_1.id = table_3.tbl1_id) AS tbl_2_name
FROM table_1

This gave an ERROR 1242. So far, I have not been able get anything to work. The result I am looking for is similar to this.

+---------------+---------------+---------------+---------------+
|table_1.id |table_1.name   |table_2.id |table_2.name   |
+---------------+---------------+---------------+---------------+
|      1    |    tbl1_1 |      1    |    tbl2_1 |
|      2    |    tbl1_2 |       |       |
|      3    |    tbl1_3 |      2    |    tbl2_2 |
|      3    |    tbl1_3 |      3    |    tbl2_3 |
|      4    |    tbl1_4 |       |       |
+---------------+---------------+---------------+---------------+

Also, I would like to be able to order the results on both table_1.name and table_2.name. If anyone has a suggestion please let me know.

A: 

So you're just trying to print out all of the related pairs? How about:

SELECT table_1.id, table_1.name, table_2.id, table_2.name
FROM table_3
INNER JOIN table_1 ON table_1.id = table_3.tbl1_id
INNER JOIN table_2 ON table_2.id = table_3.tbl2_id
ORDER BY table_1.id, table_2.id
ngroot
The query using `INNER JOIN` returned the same as the query using `LEFT JOIN`. The only results returned were those contained in `table_3`. Leaving out some of the data in `table_1`.
Brook Julias
+1  A: 

To get the rows from table_1 that have no matches in the other tables you should use an OUTER JOIN instead of an INNER JOIN:

SELECT
    table_1.id,
    table_1.name,
    table_2.id,
    table_2.name
FROM table_1
LEFT JOIN table_3 ON table_3.tbl1_id = table_1.id
LEFT JOIN table_2 ON table_3.tbl2_id = table_2.id

Result:

table_1.id  table_1.name  table_2.id  table_2.name
1           'tbl1_1'      1           'tbl2_1'    
2           'tbl1_2'                  ''          
3           'tbl1_3'      2           'tbl2_2'    
3           'tbl1_3'      3           'tbl2_3'    
4           'tbl1_4'                  ''          
Mark Byers
When using `table_1` in the `FOR` clause and the two corresponding `LEFT JOIN` statements an `ERROR 1066` is thrown.
Brook Julias
@Brook Julias: Error 1066 means *Not unique table/alias*. Avoid giving two tables the same alias and it should work. I have tested the query on your table structure. The result I posted in my answer is a copy/paste of the result I got when testing the query.
Mark Byers
A: 

Changing the LEFT JOIN to RIGHT JOIN and the order in which the two joins appear in the query solved this problem. Here is a copy of the working code;

SELECT  table_1.id,
    table_1.name,
    table_2.id,
    table_2.name
FROM    table_3
RIGHT JOIN  table_2 ON (table_3.tbl2_id = table_2.id)
RIGHT JOIN  table_1 ON (table_3.tbl1_id = table_1.id)
ORDER BY table_1.name ASC, table_2.name ASC;
Brook Julias