1

我对我认为是计算记录的简单查询有疑问:

SELECT req_ownerid, count(req_status_lender) AS total6 
FROM bor_requests
WHERE (req_status_lender = 0 AND req_status_borrower = 0) OR 
      (req_status_lender = 1 AND req_status_borrower = 1)
GROUP BY req_ownerid 
HAVING req_ownerid = 70

我认为这将计算 (req_status_lender = 0 AND req_status_borrower = 0) 和 (req_status_lender = 1 AND req_status_borrower = 1) 的所有记录,然后给我总数,但它只给我任何一个 (req_status_lender = 0 AND req_status_borrower = 0) 或 (req_status_lender = 1 AND req_status_borrower = 1)。

任何想法我做错了什么?

4

1 回答 1

2

您应该HAVING仅使用该子句来限制在上面的查询中聚合的内容 - 例如,如果您想选择 aSUM(....)COUNT(...)大于 5 的所有行,那么您将使用HAVING SUM(...) > 5

你在这里做的是一个标准WHERE条款——把它加在那里!

SELECT req_ownerid, count(req_status_lender) AS total6 
FROM bor_requests
WHERE req_ownerid = 70
      AND ((req_status_lender = 0 AND req_status_borrower = 0) OR 
           (req_status_lender = 1 AND req_status_borrower = 1))
GROUP BY req_ownerid 
于 2011-02-06T10:49:22.120 回答