how to make counting in mysql
i have a table that "users"
i want to count their salary who have开发者_如何学编程 ID 1 and 2
how i can count this in mysql
Have a look at Aggregate Functions in MySQL and GROUP BY (Aggregate) Functions
to get total salary of both
SELECT SUM(salary) FROM users WHERE id IN(1, 2);
and to get individual sum of salary
SELECT SUM(salary) FROM users WHERE id IN(1, 2) group by id;
SELECT SUM(salary) FROM users WHERE id IN(1, 2);
I shall refer you to counting rows from the mysql documentation
probably goes like:
SELECT COUNT(*) FROM users WHERE id IN (1,2);
although if you're actually trying to sum the salaries then you should refer to group by functions
SELECT id, SUM(salary) FROM users WHERE id IN (1,2) GROUP BY id;
精彩评论