Imran Hafeez Imran Hafeez - 6 months ago 9
MySQL Question

Calculate sum & min from database tables all column

I am using this code for calculate all numbers. I have added price/number (10) in all table

$SQL = "SELECT p1 + p2 + p3 + p4 - p0 as total_price from table WHERE id = '1'";


This query works for me like, first sum all table p1=10 + p2=10 ..... result 40 minus p0=2 total price 38.

but I want to subtract p0 from all columns like


"p1 = 10" - "p0 = 2" = 8

"p2 = 10" - "p0 = 2" = 8

"p3 = 10" - "p0 = 2" = 8

"p4 = 10" - "p0 = 2" = 8

and sum p1+p2+p3+p4 final result = 32

Answer

You can do (p1 + p2 + p3 + p4) - 4 * p0. Btw 4 * 8 = 32