Group and Sum MySQL

Viewed 34

calculates the total price, name, ID of all the products that are registered in a 'products' table.

this is the code:

CREATE TABLE products (
      ID_products int(10) NOT NULL,
      Name_product varchar(40) DEFAULT NULL,
      Price decimal(10,3) DEFAULT NULL,
      Stock numeric DEFAULT NULL,
      Supplier varchar(40) DEFAULT NULL,
      PRIMARY KEY (ID_products)
);

I tried many times to use GROUP BY, plus several methods but i can't get what to do.

select Price, ID_products, count(*) as Name_product
from Products 
group by ID_products;

and the table:

Table

please i need a little nudge to get out of this.

1 Answers

If you mean the sum of the price per product id try this.

SELECT SUM(Price) AS total_price FROM productos GROUP BY ID_products;

And oh please check your queries, you will surely get an error there. You used productos in your create statement but used products in your select statement.

Related