Topic awaiting preservation: PHP/MySQL query needed. :) (Page 1 of 1) |
|
---|---|
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 02-08-2006 18:51
hello, i'm in the need of an SQL query to do the following: |
Paranoid (IV) Inmate From: New Jersey, USA |
posted 02-09-2006 01:42
Does something like this get you want you want? code: SELECT customers.id, customers.name, sum( individual_orders.price * individual_orders.quantity ) AS total, individual_orders.product_id FROM customers, orders, individual_orders WHERE individual_orders.order_id = orders.id AND orders.customer_id = customers.id GROUP BY individual_orders.order_id ORDER BY total DESC
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 02-09-2006 08:33
you store the price in the individual_orders table because that's the price you sold it for. |
Paranoid (IV) Inmate From: New Jersey, USA |
posted 02-09-2006 12:27
Cool, thanks TP. |
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 02-10-2006 16:38
butcher, thank you very very much! code: SELECT cubecart_customer.customer_id, cubecart_customer.*, SUM( cubecart_order_inv.price * cubecart_order_inv.quantity ) AS total, cubecart_order_inv.productId FROM cubecart_customer, cubecart_order_sum, cubecart_order_inv WHERE cubecart_order_inv.cart_order_id = cubecart_order_sum.cart_order_id AND cubecart_order_sum.customer_id = cubecart_customer.customer_id AND ( cubecart_order_inv.productId = 6 OR cubecart_order_inv.productId = 7 OR cubecart_order_inv.productId = 8 OR cubecart_order_inv.productId = 9 ) GROUP BY cubecart_order_inv.cart_order_id ORDER BY total DESC
|
Lunatic (VI) Inmate From: under the bed |
posted 02-10-2006 17:11 |
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 02-10-2006 17:48
ok thanks. i think i got this to work: code: SELECT cubecart_order_sum.time, cubecart_customer.customer_id, cubecart_customer.*, cubecart_order_inv.price AS total, cubecart_order_inv.productId FROM cubecart_customer, cubecart_order_sum INNER JOIN cubecart_order_inv ON cubecart_order_inv.productId = 6 OR cubecart_order_inv.productId = 7 OR cubecart_order_inv.productId = 8 OR cubecart_order_inv.productId = 9 WHERE cubecart_order_inv.cart_order_id = cubecart_order_sum.cart_order_id AND cubecart_order_sum.customer_id = cubecart_customer.customer_id GROUP BY cubecart_order_inv.cart_order_id ORDER BY total DESC
|
Lunatic (VI) Inmate From: under the bed |
posted 02-10-2006 18:42 |
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 02-10-2006 21:49
thanks ok, that is grouping the customers, but how do i sum up, all prices of purchases they make? |
Lunatic (VI) Inmate From: under the bed |
posted 02-10-2006 22:19
Off the top of my head I'm not certain (and my brain is a bit fried after the day I've had at work...) |
Paranoid (IV) Inmate From: New Jersey, USA |
posted 02-11-2006 00:40
I noticed the part of the statement with code: SUM( cubecart_order_inv.price * cubecart_order_inv.quantity ) missing from your last version of the statement. Would that make the difference? |
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 02-12-2006 13:13
thanks again guys! code: SELECT cubecart_order_sum.time, cubecart_customer.customer_id, cubecart_customer.*, SUM(cubecart_order_inv.price) AS total FROM cubecart_customer, cubecart_order_sum INNER JOIN cubecart_order_inv ON cubecart_order_inv.productId = 6 OR cubecart_order_inv.productId = 7 OR cubecart_order_inv.productId = 8 OR cubecart_order_inv.productId = 9 WHERE cubecart_order_inv.cart_order_id = cubecart_order_sum.cart_order_id AND cubecart_order_sum.customer_id = cubecart_customer.customer_id GROUP BY cubecart_customer.customer_id ORDER BY total DESC
|