简体   繁体   English

在MySQL中使用内部联接是否可能?

[英]Is this possible using inner joins in MySQL?

I'm stuck on trying to build a query that will include a given order_reference cover file from the data sample below only if ALL of the print_item_qty values of the files under the same order_reference are equal to 5. 只有在相同order_reference下文件的所有print_item_qty值均等于5的情况下,我才会尝试从以下数据样本构建包含给定order_reference封面文件的查询。

Is this possible using joins or is this outside the remit of a single query? 是否可以使用联接,还是在单个查询的权限之外?

I've tried building inner joins but cannot work out how to restrict the results so that I only get a cover row when all the component parts of an order have equal values in the print_item_qty column. 我尝试构建内部联接,但无法解决如何限制结果的问题,因此,仅当订单的所有组成部分在print_item_qty列中具有相等的值时,我才获得封面行。

Here is the base SELECT query and sample data: 这是基本的SELECT查询和示例数据:

SELECT c1.`order_id`,c1.name1,c1.name2,c1.`print_item_qty`,c1.`sub_item_type`,
c1.`order_reference` FROM print_items;


+--------------+-------+---------+----------------+---------------+-----------------+
| order_id     | name1 | name2   | print_item_qty | sub_item_type | order_reference |
+--------------+-------+---------+----------------+---------------+-----------------+
| 000003201875 | Jason | Bramley | 5              | cover         | 1875            |
| 000003201875 | Jason | Bramley | 5              | inner         | 1875            |
| 000003201875 | Jason | Bramley | 1              | card          | 1875            |
| 000003201876 | Jason | Bramley | 5              | cover         | 1876            |
| 000003201876 | Jason | Bramley | 5              | inner         | 1876            |
+--------------+-------+---------+----------------+---------------+-----------------+

My desired result for the above sample data would be only the following row: 对于上面的示例数据,我期望的结果只是下面的行:

+--------------+-------+---------+----------------+---------------+-----------------+
| order_id     | name1 | name2   | print_item_qty | sub_item_type | order_reference |
+--------------+-------+---------+----------------+---------------+-----------------+
| 000003201876 | Jason | Bramley | 5              | cover         | 1876            |
 +--------------+-------+---------+----------------+---------------+-----------------+

Any help anyone could give to point me in the right direction would be greatly appreciated. 任何人能给我指出正确方向的任何帮助将不胜感激。

Many thanks. 非常感谢。

So you want to verify that you only select data for orders for which the print_item_qty = 5 for all sub_item_type in that order_reference. 因此,您想验证是否只为该order_reference中所有sub_item_type的print_item_qty = 5的订单选择数据。

To do this, use a subsquery like 为此,请使用类似的子查询

SELECT order_reference, 
MAX(print_item_qty), 
MIN(print_item_qty) 
FROM print_items
GROUP BY order_reference 
HAVING MAX(print_item_qty) = 5
AND MIN(print_item_qty) = 5

And join to your original dataset. 并加入原始数据集。 The subquery will restrict to the ids you want, and joining back will return all rows associated with the order_references for which print_item_qty = 5 for every sub_item_type, eg, 子查询将限制为您想要的ID,返回联接将返回与order_references相关联的所有行,其中对于每个sub_item_type,print_item_qty = 5,例如,

SELECT c1.`order_id`,
c1.name1,
c1.name2,
c1.`print_item_qty`,
c1.`sub_item_type`,
c1.`order_reference` 
FROM print_items AS c1
INNER JOIN (SELECT order_reference, MAX(print_item_qty), MIN(print_item_qty) FROM print_items
GROUP BY order_reference HAVING MAX(print_item_qty) = 5 AND MIN(print_item_qty) = 5) AS b
ON c1.order_reference = b.order_reference 

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

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