Post by L146705 » Fri Jun 07, 2013 1:00 am

Hi,

I'm trying to write a sql query that will give me the total price for all products in the opencart shop. So for example if I had 100 products in the shop 50 of them for £1 each and 50 of them for £2 each, the total price of all the products in the shop would be £150. Any ideas on how I can do this?

New member

Posts

Joined
Sun Feb 26, 2012 2:50 am

Post by rph » Fri Jun 07, 2013 1:47 am

SQL (assumes no database prefix):

Code: Select all

SELECT SUM(price) FROM product;
OpenCart:

Code: Select all

$this->db->query("SELECT SUM(price) FROM `" . DB_PREFIX . "product`");

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by midgette » Fri Jun 07, 2013 2:07 am

Ignoring specials, something like this should work:

Code: Select all

echo $this->db->query("select CONCAT('$', FORMAT(sum(quantity * price), 2)) as total_stock_amt from " . DB_PREFIX . "product")->row['total_stock_amt'];
Taking specials into account:

Code: Select all

echo $this->db->query("
select CONCAT('$', FORMAT(sum(p.`quantity` * case when ps.Price is null then p.`price` else ps.price end), 2)) as total_stock_amt
from " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_special ps on p.product_id = ps.product_id
where	case when ps.Price is null then 1 = 1 else ps.date_start <= curdate() and ps.date_end >= curdate() end ")->row['total_stock_amt'];
Good luck.
Midgette

Newbie

Posts

Joined
Sun Apr 28, 2013 4:16 pm
Who is online

Users browsing this forum: Bing [Bot] and 30 guests