Let us first create a table −
mysql> create table DemoTable1489 -> ( -> ProductId int, -> ProductPrice int -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1489 values(100,900); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1489 values(115,1000); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1489 values(119,2100); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1489 values(125,2100); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1489 values(128,2900); Query OK, 1 row affected (0.25 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1489;
This will produce the following output −
+-----------+--------------+ | ProductId | ProductPrice | +-----------+--------------+ | 100 | 900 | | 115 | 1000 | | 119 | 2100 | | 125 | 2100 | | 128 | 2900 | +-----------+--------------+ 5 rows in set (0.00 sec)
Here is the query to get sum of columns with conditions −
mysql> select ProductId div 10,sum(ProductPrice) from DemoTable1489 -> group by ProductId div 10;
This will produce the following output −
+------------------+-------------------+ | ProductId div 10 | sum(ProductPrice) | +------------------+-------------------+ | 10 | 900 | | 11 | 3100 | | 12 | 5000 | +------------------+-------------------+ 3 rows in set (0.00 sec)