Group and Sum MySQL

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:

Group and Sum MySQL

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x