简体   繁体   English

使用SQL(mysql)将一个子查询的结果附加到另一个子查询的结果

[英]With SQL (mysql) Append the results of one sub-query to that of another

I have two mysql tables. 我有两个mysql表。 And essentially two queries (one to each table) that I want to combine within a single SQL Query. 基本上我想在一个SQL查询中组合两个查询(每个表一个)。 Seems like it should be easy (kind of like an inner Join) But inner join is DUPLICATING non-uniqe values from table 2 into my results array... I dont want them duplicated... I want those duplicates from table/query 2 combined into the result record of query1 好像它应该很容易(有点像内部Join)但是内连接是将表2中的非uniqe值重复到我的结果数组中...我不希望它们重复...我想要从表/查询2重复这些合并到 query1的结果记录中

Query 1 Gets records from table 1. Results are unique. 查询1从表1中获取记录。结果是唯一的。 (one ID returns one record) It's simply returning all fields on records where an ID is equal to one of my conditions. (一个ID返回一个记录)它只是返回ID等于我的一个条件的记录上的所有字段。 [Im using IN instead of a bunch of OR's) [我使用IN而不是一堆OR)

$sid = "'M-179','M-242','M-231','Q-2thru5'" ; 
$query = "SELECT * FROM table1 WHERE IN ($sid)`

Query 2 gets records from table 2. But results are NOT unique. 查询2从表2中获取记录。但结果不是唯一的。 One ID can return many records. 一个ID可以返回许多记录。

$query2 = "SELECT extra_data, pub_url FROM table2 WHERE IN ($sid)";

So I want EACH extra_data & pub_url field from ALL returned records just slapped onto the end of the query 1 result. 所以我想从ALL返回的记录中获取EACH extra_data和pub_url字段,这些记录只是打到查询1结果的末尾。 Am I making sense? 我有道理吗? So the result would look something like this... 所以结果看起来像这样......

[0] => Array
        (
            * all my returned fields from the 
             record returned by query 1 here
            $row['extra_data']  
            $row['pub_url']  <-returned record from query 2
            $row['extra_data'] 
            $row['pub_url']  <-another returned record from query 2
            $row['extra_data'] 
            $row['pub_url']  <-any another returned 
                              record from query 2, etc..
        )

What do you mean by combining the result? 结合结果是什么意思?

Do you want the results from query2 to appear in the same relevant rows but just extra columns, or you want them to appear as new rows. 您是否希望query2的结果显示在相同的相关行中,但只显示额外的列,或者您希望它们显示为新行。

For the first case, you will have to use JOIN . 对于第一种情况,您将不得不使用JOIN

But I have a feeling that what you want to do is the second case. 但我有一种感觉,你想要做的是第二种情况。 In this case, you will have to use UNION . 在这种情况下,您将不得不使用UNION Note, however, that in this case the columns of the two queries must match. 但请注意,在这种情况下,两个查询的列必须匹配。 So the union of two queries would look like this: 所以两个查询的联合看起来像这样:

-- pseudo code only
SELECT extra_data, pub_url FROM table1 WHERE IN ($sid)
union
SELECT extra_data, pub_url FROM table2 WHERE IN ($sid)

Try something similar to this, which would join the results of table 1 to table 2 on the column containing the sid value. 尝试类似于此的东西,它会将表1的结果连接到包含sid值的列的表2。

SELECT a.*, b.extra_data, pub_url
FROM table1 a
left outer join table2 b on a.sid = b.sid 
WHERE a.sid IN ($sid)

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

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