简体   繁体   English

MYSQL MAX / COUNT / IN / DATE

[英]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。

View Table 查看表

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: 您的查询有一些错误:

  • You closed the subquery parentheses after 'no_hits' . 您在'no_hits'之后关闭了子查询括号。
  • You didn't supply a table alias for the subquery. 您没有为子查询提供表别名。 Every derived table must have a table alias. 每个派生表必须具有表别名。
  • You didn't close the parentheses for the DATE_SUB() function. 您没有关闭DATE_SUB()函数的括号。
  • You used COUNT() where I think you should use SUM() , if you want the total of hits per zone. 如果希望每个区域的总点击数,则在我认为应该使用SUM() COUNT()地方使用了COUNT()
  • You aren't associating the subtotal of hits with each zone; 您没有将点击数的小计与每个区域相关联; your subtotal is for the whole table. 您的小计是整个桌子的总和。
  • You used string delimiters ( '' ) 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 1ORDER BY DESC排序子句来仅获取最大值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM