Mysql
 sql >> Base de données >  >> RDS >> Mysql

MySQL sélectionne et calcule la valeur de plusieurs colonnes

Pourquoi utilisez-vous la clause "where" ?

mysql> select * from test;
+------------+------------+----------+
| request_id | unit_value | quantity |
+------------+------------+----------+
|          1 |          3 |        2 |
|          1 |         19 |        1 |
|          2 |       6.76 |     0.75 |
|          2 |         18 |      0.5 |
+------------+------------+----------+
4 rows in set (0.00 sec)

mysql> SELECT request_id, sum(unit_value * quantity) as x from test group by request_id;
+------------+--------------------+
| request_id | x                  |
+------------+--------------------+
|          1 |                 25 |
|          2 | 14.070000171661377 |
+------------+--------------------+
2 rows in set (0.00 sec)