sql >> Base de Datos >  >> RDS >> Mysql

MySQL selecciona y calcula el valor de varias columnas

¿Por qué usas la cláusula "dónde"?

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)