[英]MYSQL MAX/COUNT/IN/DATE
Hope you can advise. 希望你能指教。 Im pulling my hair out on a query and hope you can advise.
我在询问时拔掉了头发,希望您能提出建议。
I have a table where I want to select MAX from an inner count between dates with an IN. 我有一个表格,我想从带有IN的日期之间的内部计数中选择MAX。
Here is what I have tried but with no luck at all. 这是我尝试过的,但是没有运气。
SELECT MAX(no_hits) from (SELECT count(hits) AS 'no_hits' ) FROM stats WHERE 'date' >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH AND zones_code IN('011242077793513596890', '011242077783866125432'))
So basically I only want one no_hits returned for the best performing zone. 因此,基本上我只希望返回一个no_hits以获得最佳性能区域。
Hope you can advise on where im going wrong. 希望您能就即时通讯出错的地方提出建议。
And thank you if you cam 谢谢你的帮忙
SELECT `zones_code`, SUM(`hits`) AS `no_hits`
FROM `stats`
WHERE `date` >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
GROUP BY `zones_code`
ORDER BY `no_hits` DESC
LIMIT 1
Your query is this: 您的查询是这样的:
SELECT MAX(no_hits)
FROM (SELECT count(hits) AS 'no_hits' ) FROM stats
WHERE 'date' >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH
AND zones_code IN('011242077793513596890', '011242077783866125432')
);
Here are some things wrong with your query: 您的查询有一些错误:
'no_hits'
. 'no_hits'
之后关闭了子查询括号。 DATE_SUB()
function. DATE_SUB()
函数的括号。 COUNT()
where I think you should use SUM()
, if you want the total of hits per zone. SUM()
COUNT()
地方使用了COUNT()
。 ''
) for 'date'
instead of identifier delimiters (ie back-quotes). ''
)为'date'
的标识,而不是分隔符(即反引号)。 You're comparing the literal string 'date'
to a date value, when you mean to compare the column date
to a date value. date
与日期值进行比较时,您正在将文字字符串'date'
与日期值进行比较。 The query in @chaos's answer is close, but I think you should use SUM()
: @chaos的答案中的查询很接近,但是我认为您应该使用
SUM()
:
SELECT `zones_code`, SUM(`hits`) AS `no_hits`
FROM `stats`
WHERE `date` >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
GROUP BY `zones_code`
ORDER BY `no_hits` DESC
LIMIT 1;
The result is zone_code 011242077793513596890, with a total of 255 hits. 结果是zone_code 011242077793513596890,总共255个匹配。
PS: When you ask questions online, please supply in textual format enough code and data for people to test easily. PS:当您在线提问时,请以文本格式提供足够的代码和数据,以便人们轻松进行测试。 You supplied a screenshot of some sample data, and without showing the table creation code.
您提供了一些示例数据的屏幕截图,但未显示表创建代码。 This is not as helpful as if you had supplied a valid
CREATE TABLE
statement and a sample INSERT
statement to populate it. 就像您提供了一个有效的
CREATE TABLE
语句和一个示例INSERT
语句来填充它一样,它没有帮助。
CREATE TABLE IF NOT EXISTS `stats` (
`id` int(11) NOT NULL,
`zones_code` char(21) default NULL,
`date` date default NULL,
`hits` int(11) default NULL,
PRIMARY KEY (`id`)
);
INSERT INTO stats VALUES
(10, '011242077793513596890', '2009-05-11', 13),
(12, '011242077793513596890', '2009-05-12',235),
(24, '011242077793513596890', '2009-05-13', 2),
(32, '011242077793513596890', '2009-05-14', 5),
(17, '011242077783866125432', '2009-05-12',165),
(22, '011242077783866125432', '2009-05-13', 2),
(30, '011242077783866125432', '2009-05-14', 5),
(19, '011242077743853330663', '2009-05-12', 61),
(20, '011242077737314753388', '2009-05-12', 54),
(28, '011242077737314753388', '2009-05-13', 7),
(36, '011242077737314753388', '2009-05-14', 31),
(14, '011242077730456603312', '2009-05-12',240),
(26, '011242077730456603312', '2009-05-13', 2),
(34, '011242077730456603312', '2009-05-14', 5);
The above is what I had to type in based on your screen shot! 以上是我必须根据您的屏幕截图输入的内容!
Do yourself a favor and make it easier for people to help you. 帮自己一个忙,使人们更容易为您提供帮助。
我将使用带有LIMIT 1
的ORDER BY DESC
排序子句来仅获取最大值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.