I have these two tables
I want to get the sum of the states of all the users, for example
user1: estado=1
user1: estado=0
user2: estado=1
user1: estado=0
user1: estado=1
user1: estado=1
user2: estado=0
user2: estado=1
I want the sum, the result would be:
for user 1: state sum 1 = 3, state sum 0 = 2,
for user 2: state sum 1 = 2, state sum 0 = 2
I was trying:
objBD.query('SELECT * from USUARIO ', function(err, rows, fields) {
//debo usa un for para la consulta de abajo
objBD.query('SELECT SUM(estado) from USUARIO u INNER JOIN INTENTO i ON u.ID_U=i.ID_U where estado=? and u.ID_U=?',[1,rows[0].ID_U], function(err, rows, fields) {
console.log(rows);
});
});
My query is only meant for the value of estado=0
, I should do the same for estado=1
, I do not think it's the best way, I want you to help me create a cleaner query.