Jump to content

Need Help with formulating a Query


vincej

Recommended Posts

Hi - I'm struggling formulating a query:

 

I have purchase orders. Each purchase order can have several line items. My table holds line items by purchase order id . See sample below.

 

+ Options

orderid orderdate customerid prodid price quantity notes dellocation id name

JAC1297 1320251297 5 22 155.25 1 NULL varsity 22 Turkey Jumbo Pack

JAC1297 1320251297 5 20 120.35 1 NULL varsity 20 Chicken Variety Pack

JAC1297 1320251297 5 25 50.00 1 NULL varsity 25 50 piece bag of Scalops

JAC6251 1320176251 5 25 50.00 1 NULL varsity 25 50 piece bag of Scalops

JAC0209 1319750209 5 22 155.25 1 NULL varsity 22 Turkey Jumbo Pack

JAC0209 1319750209 5 23 20.00 1 NULL varsity 23 Whole Chicken

 

I'm struggling to build a query which aggregates all the line items for a given orderid so that I can do a php foreach and echo the orderid ONCE with the associated product / price quantity etc underneath.

 

The query I have built only grabs the first product for each orderid and leaves out the rest, see below:

 

SELECT orderid,  order.orderdate, order.prodid, order.price, order.quantity, products.name, sum( order.price * order.quantity ) AS 'Sales'
FROM `ORDER` , `products`
WHERE order.customerid = '5'
    AND order.prodID = products.id
AND order.Status = 'open'
   	GROUP BY orderid
ORDER BY order.orderdate DESC
LIMIT 0 , 30

 

 

 

MANY MANY Thanks to all those who might be able to offer any ideas !

 

 

Using MySQL 5.5

 

Link to comment
Share on other sites

SELECT <fields you need> FROM ORDER
WHERE ORDER.id=<ORDER ID HERE>

 

or

 

SELECT <fields you need> FROM ORDER
LEFT JOIN ORDER.id=PRODUCT.OrderID

 

That would be the core of what you need. Furthermore, you might want to consider using INTs as your Order ID instead of Strings. INTs have handy things with them, such as AUTO_INCREMENT. Don't know if you can do indexing with Strings, I think not.

 

Good luck!

Wiro Blangkon.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.