mysql subtract two rows from column and places into an alias
问题 I have table meter_readings with columns: id , date_taken , kwh . I'm trying to subtract two rows in kwh column together and put the results into an alias called consumption . I'm using: SELECT id, kwh COALESCE(kwh-(SELECT kwh FROM meter_readings WHERE id= id+1), kwh) AS consumption FROM meter_readings; What I get back in consumption alias is simple the same as the original kwh : id date_taken kwh consumption 1 2013-01-01 4567.89 4567.89 2 2013-01-08 4596.71 4596.71 3 2013-01-15 4607.89 4607