Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL Query for 4 different tables
    text
    copied!<p>I apologise in advance if this might seem simple as my assignment needs to be passed in 2 hours time and I don't have enough time to do some further research as I have another pending assignment to be submitted tonight. I only know the basic MYSQL commands and not these types. And this is one of the final questions left unanswered and is making me go nuts even if i have already read the JOIN documentation . Please help.</p> <p>Say I have 4 tables</p> <pre><code> _______________ _______________ _______________ _______________ | customers | | orders | | ordered_items | | products | |_______________| |_______________| |_______________| |_______________| |(pk)customer_id| | (pk)order_id | | (pk)id | |(pk)product_id | | first_name | |(fk)customer_id| | (fk)order_id | | name | | last_name | | date | |(fk)product_id | | description | | contact_no | | | | quantity | | price | |_______________| |_______________| |_______________| |_______________| </code></pre> <p>How would i be able to query all the products ordered by (eg: customer_id = '5')</p> <p>I only know basic SQL like straight forward queries on 1 table and joins from 2 different, but since its 4 different tables having different relations to one another, how would i be able to get all the products ordered by a particular customer id?</p> <p>Because its like get all the products from ordered products where order_id = (* orders by customer_id = 5).</p> <p>But what can be an optimised and best practice way in doing this type of query</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