组合两个选择查询

时间:2010-09-08 20:04:22

标签: mysql join subquery left-join

在我使用两个查询的那一刻,最初调用一个,第二个调用在第一个结果的循环中调用。我想结合两个查询,但到目前为止还没有。查询提取的表格是:

+--------------+    +--------------+    +--------------------------+
|    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  |    +----+---------+    +----+----------+----------+
+----+---------+

table_1中的table_2table_3之间存在多对多关系。到目前为止,我一直在使用单独的查询。一个查询返回table_1的所有内容,另一个查询返回table_2通过table_1连接的table_3值。但是,我想废除循环并减少发送到服务器的查询量。我尝试过使用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)

这几乎非常想要我想要的,除了它只返回table_3中的值,而忽略了table_1中的一些值。我尝试过使用子查询:

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

这给了ERROR 1242。到目前为止,我还没有得到任何工作。我正在寻找的结果与此相似。

+---------------+---------------+---------------+---------------+
|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 |       |       |
+---------------+---------------+---------------+---------------+

此外,我希望能够在table_1.nametable_2.name上订购结果。如果有人有任何建议,请告诉我。

3 个答案:

答案 0 :(得分:1)

要从table_1获取其他表中没有匹配项的行,您应该使用OUTER JOIN而不是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

结果:

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'                  ''          

答案 1 :(得分:0)

所以你只是想打印出所有相关配对?怎么样:

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

答案 2 :(得分:0)

LEFT JOIN更改为RIGHT JOIN以及查询中出现两个联接的顺序解决了此问题。这是工作代码的副本;

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;