MySQL 最小和最大日期以及 Group By 和 SubString

发布于 2024-12-26 13:12:22 字数 1421 浏览 1 评论 0原文

我正在开发一个项目,我需要从数据库获取最小和最大日期,以及将字段的第一部分子串为字符并按该字段分组。所以基本上在我的数据库中

2012-01-01 未知的用户名/密码尝试登录。用户:admin

2012-01-01 添加用户帐户失败。 MySQL 错误:某些我的 sql 错误未知

2012-01-02 添加用户帐户失败。 MySQL 错误:某些我的 sql 错误未知

2012-01-04 尝试登录未知的用户名/密码。用户: 管理员

,该查询在不显示日期的情况下工作。

SELECT ala_type, partMsg, 
wholeMsg, ala_level, ala_page, COUNT(wholeMsg) AS msgCount FROM
(
SELECT ala_type, SUBSTRING_INDEX(ala_message, ':', 1) AS partMsg, ala_message AS wholeMsg,
ala_level, ala_page FROM alarms a) p
GROUP BY partMsg

此位按预期正确分组,但如果我尝试添加 min(date_field),则它只显示一条记录。

以下是我尝试过的查询

选择 ala_date、min(ala_date)、max(ala_date)、ala_type、partMsg、 WholeMsg, ala_level, ala_page, COUNT(wholeMsg) AS msgCount FROM ( 选择ala_date,min(ala_date),max(ala_date),ala_type, SUBSTRING_INDEX(ala_message, ':', 1) AS partMsg, ala_message AS WholeMsg、ala_level、ala_page FROM 警报 a) p GROUP BY partMsg

如果我没有将 ala_date 放在第二个语句中,而只有 min(ala_date),则表示找不到字段 ala_date。但是,当我运行查询时,我只得到一条记录,我希望看到以下内容

2012-01-01 2012-01-04 未知的用户名/密码尝试登录。用户:2

2012-01-01 2012-01-02 添加用户帐户失败。 MySQL 错误:>某些mysql错误2

2和结尾的预期结果是计数该消息发生的次数。

我实际得到的是:

2012-01-01 2012-01-01 2012-01-01 添加用户帐户失败...1

现在仅显示 1 条记录,并且最小值和最大值相同,计数为 1 而不是 2。

什么我能做些什么来得到我期望得到的东西吗?

感谢您提供的任何帮助。

I am working on a project where I need to get the minimum and maximum date from a database as well substringing the first part of a field up to a character and grouping by this field. So basically in the database I have

2012-01-01 Unknown username/password attempted to login. User: admin

2012-01-01 Failed to add user account. MySQL Error: certain my sql
error Unknown

2012-01-02 Failed to add user account. MySQL Error: certain my sql
error Unknown

2012-01-04 Unknown username/password attempted to login. User:
administrator

Below is the query that I am using which is working without showing the date

SELECT ala_type, partMsg, 
wholeMsg, ala_level, ala_page, COUNT(wholeMsg) AS msgCount FROM
(
SELECT ala_type, SUBSTRING_INDEX(ala_message, ':', 1) AS partMsg, ala_message AS wholeMsg,
ala_level, ala_page FROM alarms a) p
GROUP BY partMsg

This bit is grouping correctly as expected but if I try to add a min(date_field) it then only shows one record.

Below is the query that I have tried

SELECT ala_date, min(ala_date), max(ala_date), ala_type, partMsg,
wholeMsg, ala_level, ala_page, COUNT(wholeMsg) AS msgCount FROM (
SELECT ala_date, min(ala_date), max(ala_date), ala_type,
SUBSTRING_INDEX(ala_message, ':', 1) AS partMsg, ala_message AS
wholeMsg, ala_level, ala_page FROM alarms a) p GROUP BY partMsg

If I don't put ala_date in the second statement and just have min(ala_date) it says that it can't find the field ala_date. However, when I run the query I am only getting one record and I would have expected to see the following

2012-01-01 2012-01-04 Unknown username/password attempted to login.User: 2

2012-01-01 2012-01-02 Failed to add user account. MySQL Error: > certain mysql error 2

The 2 and the end of the expected result is the count how many times the message occurred.

What I am actually getting is:

2012-01-01 2012-01-01 2012-01-01 Failed to add user account... 1

It is now only showing 1 record and the min and max only are the same and the count is 1 instead of 2.

What could I do to get what I am expecting to get.

Thanks for any help you can provide.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南烟雨〆相思醉 2025-01-02 13:12:22

你会做类似的事情(我的mysql生锈了),但理论是合理的。

Select SomeDate,SomeMessage From SomeTable
inner join (
Select Min(SomeDate) as SomeDate From SomeTable) earliest
On earliestDate.SomeDate = SomeTable.SomeDate

即找到每个组的最小日期,然后连接回原始表以查找也在该日期的记录。

You would do something like (my mysql is rusty) but the theoiry is sound.

Select SomeDate,SomeMessage From SomeTable
inner join (
Select Min(SomeDate) as SomeDate From SomeTable) earliest
On earliestDate.SomeDate = SomeTable.SomeDate

ie find the minimum date for for each group then join back to the original table to find the records that were also on that date.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文