I have this query using express and mysql, the query in the database works and returns a column with the title "total" and a row with the sum of several expenses. I need to get the number that appears in the row to later show it on the screen with an ajax query, but with the code like this if I enter the link it does not return anything. What can I do? thanks
// GET total spended
router.get('/total', (req, res) => {
mysqlConnection.query("SELECT (CTE.INCOME - CTE.OUTCOME) AS TOTAL FROM ( SELECT SUM(CASE WHEN operaciones.Tipo= 'Ingreso' THEN COALESCE(operaciones.Monto,0) ELSE 0 END) AS INCOME, SUM(CASE WHEN operaciones.Tipo='gasto' THEN COALESCE(operaciones.Monto,0) ELSE 0 END) AS OUTCOME FROM operaciones ) CTE;", (err, rows, fields) => {
if (!err) {
res.json(rows);
} else {
console.log(err);
}
});
});