mysql SUM(value)其中SUM(value)> 2500

时间:2018-04-12 02:52:42

标签: mysql group-by sum

我有一个交易表(简化了所需的列):

id    client_id    value
1     1            200
2     2            150
3     1            350
4     2            100

我想返回client_id,其中sum(value)> = 500。

我试过了: SELECT client_id, sum(value) as total FROM transactions WHERE total >= 500; 该查询返回"#1054 - 未知列'总计'在' where子句'"

我也尝试过: SELECT client_id, sum(value) FROM transactions WHERE sum(value) >= 500; That query returns "#1111 - Invalid use of group function"

我也尝试过: SELECT client_id, sum(value) FROM transactions WHERE sum(value) >= 500 GROUP BY client_id; 该查询还返回"#1111 - 无效使用组功能"

我已经花了几个小时寻找并且无法用正确的问题来查找我的查询,感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

其他答案都没有给出确切的查询,所以这里是:

[Test]  
public async Task HttpPost_CreatePost_When_Model_Is_Invalid_Returns_Error()
{
    // Arrange
    var expectedErrorMessage = "Invalid input";
    var model = new SavePostViewModel()
    {
        //Author = "Tamim Iqbal",
        Content = "New Content",
        IsPublished = true,
        //Subject = "New Subject",
        //PostDate = Helper.LocalDateToday,
    };

    // Act
    var result = await _blogController.CreatePost(model) as ViewResult;
    var actualErrorMessage = result.ViewData["ERROR_MESSAGE"];

    // Assert
    Assert.AreEqual(expectedErrorMessage, actualErrorMessage);
}

答案 1 :(得分:0)

... HAVING SUM(value) >= 500

在SQL中,WHERE适用于单个行值,而HAVING适用于跨行的聚合值。