简体   繁体   中英

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.

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.

Here is the base SELECT query and sample data:

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.

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,

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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