Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>in case you are looking to list all the items <b>ordered</b> by order date (not grouped) then it would be like this:</p> <pre><code>SELECT * FROM `order` LEFT JOIN cart ON cart.cart_id=`order`.cart_id ORDER BY order_date ASC </code></pre> <p>or :</p> <pre><code>SELECT * FROM cart LEFT JOIN `order` ON `order`.cart_id=cart.cart_id ORDER BY order_date ASC </code></pre> <p>as from the structure it seems that cart can contain several orders (while order can not contain more carts) - that's where you must tell us, whether it is 1:n relation (of those two tables) or whether it's 1:1 (in which case you do not need the cart_id and can use the order_id instead as the linking element in both tables)</p> <p>in case you want to "group" it somewhat, then you will have to sacrifice the detail (which is to be grouped together) grouping is useful, if you wanted to see the total values of all the orders - but doing so, you would only see the total value but NOT the items from each order (not in a line by line view)</p> <p><b>EDIT:</b><br/><i>[ sorry i forgot.. it's a 1:n relation.. i also need to display the ordered items from each customer.. is that possible ? :) ]</i></p> <p>in that case the structure does not seem very sound assuming the master table is the "order" then the table with detail should contain refference to the master table:</p> <p>CART Table:</p> <ul> <li>cart_id</li> <li>order_id <sup>(added)</sup></li> <li>product_id</li> <li>user_id</li> <li>prod_name</li> <li>price</li> <li>quantity</li> <li>image</li> </ul> <p>ORDER Table:</p> <ul> <li>order_id</li> <li><strike>cart_id</strike> <sup>(removed)</sup></li> <li>user_id</li> <li>total</li> <li>customer_name</li> <li>order_date</li> </ul> <p>Again your question isn't very clear. Do you want to see the items a user purchased grouped by the items (that is, if they buy a pencil today rubber tomorrow and another pencil the day after tomorrow, you will see two lines of result similar to: </p> <pre><code>item | qty -------+---- pencil | 2 rubber | 1 </code></pre> <p>then use:</p> <pre><code>SELECT `order`.*, product_id, product_name, SUM(quantity) AS qty FROM `order` LEFT JOIN cart ON cart.order_id=`order`.order_id GROUP BY product_id ORDER BY product_name </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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