小编lew*_*lsh的帖子

MySQL查询中的复杂算术

我有一张表如下:

product | quantity | price | gift | giftprice
--------|----------|-------|------|----------
1       | 2        | 9.99  | 0    | 4.99
2       | 3        | 3.50  | 1    | 2.25
3       | 1        | 4.75  | 1    | 1.50
Run Code Online (Sandbox Code Playgroud)

我希望有一个SQL查询,它会给我一个数字,给我一个数量乘以价格的所有记录的总和,只有当'gift'字段设置为'时,才将giftprice添加到乘法前的价格中1.

伪代码

foreach(record){
   if(gift == 1){ linetotal = (price + giftprice) * quantity; }
   else { linetotal = price * quantity; }
   total = total + linetotal;
}
Run Code Online (Sandbox Code Playgroud)

mysql sql math

0
推荐指数
1
解决办法
4091
查看次数

标签 统计

math ×1

mysql ×1

sql ×1