I am stuck on the query synatax for the following scenario.
Lets say I have a table structured like:
id - name - count
Now I would like to get records whose count values sum to a particular number.
Example.
1 - A - 3
2 - A - 2 3 - A - 5 4 - B - 1 5 - C - 2And I would like to get only records (tuples) whose count value added together gives me number 10.
The result should 开发者_开发百科give me:
1 - A - 3
2 - A - 2 3 - A - 5I think you can accomplish this with a nested query, like so:
SELECT *
FROM (
SELECT *, SUM(`count`) AS `sum`
FROM `table`
GROUP BY `name`
)
WHERE `sum` = '10'
select *
from tableName t
where 10 = (
select sum(count)
from tableName
where name = t.name
)
精彩评论