简体   繁体   中英

Use Mysql to calculate a percentage of a total

I'd like to charge £6.75 or 3% of a Net Order total - whichever is the greater.

£225 is the switching point to 3%.

How does this look:

if ($subtotal < 225) return 6.75; //where 6.75 is min charge
if ($subtotal > 225) {
$shipping = ($subtotal /100) * 3; //get 3%
$shipping = ROUND($shipping, 2); //round the figure
return $shipping;
}

Thanks

You can try like this

select IF(subtotal < 225 , 6.75, subtotal*.03) from table

Tested like this:

select IF(100 < 225 , 6.75, (100*.03)); // will return 6.75
select IF(300 < 225 , 6.75, (300*.03)); // will return 9

I would simply do:

$shipping = greatest(0.03*subtotal, 6.75)

I wouldn't build the constant 225 into the logic. If something changes, no one will know whether or not the constant should also be changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM