0

There are so many of these questions, it's ridiculous. However, even after reading many of them, I still have come short of my wanted result.

Using, PHP, I poll MySQL database using the following query

SELECT COUNT(`NumRecip`) AS `Total`
FROM `Messaging`
WHERE `Type`='Response'
AND `datetime`>='2013-09-01 00:00:00'

I have also attempted to add GROUP BY NumRecip And attempted COUNT(*) but I get the result 55. (If you were trying this, obviously won't be the same answer). The 55 is not at all arbitrary. The 55 is the number of rows that fit this context. The answer should be somewhere between 150 and 250 (Just guessing, but whatever). I have used the Max(NumRecip) and resulted 66 (which is the max btw) but I want the entire total of each and every value.

Long story short... I want a query that will count all the 2s, 3s, 6s, and the one 66 and get my final answer, not the 55 (number of rows) or the 66 (max number).

All queries I attempted:

SELECT COUNT(`NumRecip`) AS `Total`
FROM `Messaging`
WHERE `Type`='Response'
AND `datetime`>='2013-09-01 00:00:00'

--

SELECT COUNT(*) AS `Total`
FROM `Messaging`
WHERE `Type`='Response'
AND `datetime`>='2013-09-01 00:00:00'
GROUP BY `NumRecip`

--

SELECT COUNT(`NumRecip`) AS `Total`
FROM `Messaging`
WHERE `Type`='Response'
AND `datetime`>='2013-09-01 00:00:00'
GROUP BY `NumRecip`

--

SELECT COUNT(*) AS `Total`
FROM `Messaging`
WHERE `Type`='Response'
AND `datetime`>='2013-09-01 00:00:00'
GROUP BY `NumRecip`

--

SELECT MAX(`NumRecip`) AS `Total`
FROM `Messaging`
WHERE `Type`='Response'
AND `datetime`>='2013-09-01 00:00:00'

I can do a mass query, do a while($row=mysql_fetch_assoc($query)){ and just keep adding the NumRecip to a total variable, but I am under the assumption there is a one-query option to do this, and therefor, faster way.

4

1 回答 1

2

Count 只显示行数。如果要将所有数字相加,请使用 SUM()

SELECT SUM(`NumRecip`) AS `Total`
 FROM `Messaging`
 WHERE `Type`='Response'
 AND `datetime`>='2013-09-01 00:00:00'
于 2013-10-03T20:24:17.373 回答