Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect table entries belonging to combinations of entries in another table (mysql)
    text
    copied!<p>Regarding MySQL queries, how can I get all table entries that belong to combinations of entries in another table?</p> <p><em>Background:</em> I would like to count orders which consist of order items. Each order item has a state like 'canceled' or 'delivered'. There are partial deliveries, so that there can be both delivered and canceled orders in one order. I would like to count the net orders and I would like to know how many orders have items with more than one item status. </p> <pre><code>Order Number | Order Item | Status X0001 | Item | delivered X0001 | Item | delivered X0002 | Item | delivered X0002 | Item | canceled X0002 | Item | delivered X0003 | Item | delivered </code></pre> <p>I have 3 valid orders here and one order with delivered/canceled. How can I ask for all orders which have at least one delivered and one canceled item?</p> <p>As I am very new to MySQL I am basically looking for the right approach. Do I need subqueries or joints for this?</p> <p>Edit: First of all, sorry for the late reply. And sorry again because my question was obviously misleading.</p> <p>There are three tables involved: 'order', 'order_item' and 'order_item_status'.</p> <p>'order' and 'order_item' are linked through 'order_item.fk_order' and 'order.id_order'. </p> <p>'order_item' and 'order_item_status' are linked through 'order_item.fk_order_item_status' and 'order_item_status.id_order_item_status'. </p> <p>You have been very helpful so far but I am still a bit stuck as I do not know how to finally count by combination. The perfect result would be something like that:</p> <pre><code>{shipped} | 34 {canceled} | 12 {shipped, canceled} | 8 {closed, canceled} | 4 {closed} | 27 ... | ... </code></pre> <p>But I don't know how to deal with combinations in a query. Maybe you have some more helpful hints for me ...</p> <p>Thank you very much.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload