开发者

Query syntax for MySQL

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 - 2

And 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 - 5


I 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
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜