简体   繁体   English

结合两个选择查询

[英]Combining two select queries

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 . table_3 table_1table_2之间存在多对多关系。 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 . 一个查询返回table_1所有内容,第二个查询返回通过table_3连接到table_1table_2的值。 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 : 我尝试使用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 . 除了我只返回table_3的值,而table_3table_1某些值之外,这返回了我想要的table_3 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 . 这产生了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 . 另外,我希望能够在table_1.nametable_2.name上对结果进行table_2.name If anyone has a suggestion please let me know. 如果有人有建议,请告诉我。

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: 要从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

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

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

Changing the LEFT JOIN to RIGHT JOIN and the order in which the two joins appear in the query solved this problem. LEFT JOIN更改为RIGHT JOIN以及两个联接在查询中出现的顺序解决了此问题。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM