Mysql唯一记录,其中存在多个记录
我正在与一个Mysql电话搏斗,希望借用你的专业知识 我相信我想要的可能只有两个选择,而我还没有做其中一个,我正努力想办法解决这个问题 我有一张这样的桌子:Mysql唯一记录,其中存在多个记录,mysql,Mysql,我正在与一个Mysql电话搏斗,希望借用你的专业知识 我相信我想要的可能只有两个选择,而我还没有做其中一个,我正努力想办法解决这个问题 我有一张这样的桌子: +------------------+----------------------+-------------------------------+ | username | acctstarttime | acctstoptime | +------------------
+------------------+----------------------+-------------------------------+
| username | acctstarttime | acctstoptime |
+------------------+----------------------+-------------------------------+
| bill | 22.04.2014 | 23.04.2014 |
+------------------+----------------------+-------------------------------+
| steve | 16.09.2014 | |
+------------------+----------------------+-------------------------------+
| fred | 12.08.2014 | |
+------------------+----------------------+-------------------------------+
| bill | 24.04.2014 | |
+------------------+----------------------+-------------------------------+
我只想从用户名栏中选择唯一的记录,即我只想为bill选择一条记录,我需要一条具有最新开始日期的记录,前提是它们不在最近三个月结束日期对我不重要,否则我不需要任何数据。总之,我需要的是最近开始日期超过3个月的人
我当前使用的命令是:
SELECT DISTINCT(username), ra.acctstarttime AS 'Last IP', ra.acctstoptime
FROM radacct AS ra
WHERE ra.acctstarttime < DATE_SUB(now(), interval 3 month)
GROUP BY ra.username
ORDER BY ra.acctstarttime DESC
因为这是账单的最新开始日期,但该开始日期不在最近三个月内。希望这将有助于澄清。非常感谢你迄今为止的帮助
这是另一个例子。如果账单的acctstartdate显示为4月份条目,那么我可以添加过去三个月的where子句,这将给出我想要的结果。尝试以下操作:
select t.*
from radacct t
join (
select ra.username, max(ra.acctstarttime) as acctstarttime
from radacct as ra
WHERE ra.acctstarttime < DATE_SUB(now(), interval 3 month)
) s on t.username = s.username and t.acctstarttime = s.acctstarttime
SQLFiddle
MySQL 5.5
根据目前的文本,我从两个方面看待这个问题:
我只想要比尔的一个记录,我需要一个最新的开始日期,前提是他们在过去三个月结束日期对我来说不重要,否则我不需要任何数据
结构
资料
我们想要什么
质疑
解释
对于最近的3个月,让我们了解每个用户的最长开始日期。为了将记录限制在最近3个月内,我们使用where date\u start>date\u subnow,间隔3个月,并通过用户名查找每个用户的最大开始日期
然后,我们根据用户和最大日期将主数据与这个小子集连接起来,以获得所需的结果
另一个角度
如果我们不想查看最近3个月,而是想查找每个用户的最新日期,我们将查看此类数据:
我们想要什么
质疑
希望您可以根据自己的喜好更改这些查询
编辑
根据你的解释,问题是这样的
SQLFiddle:
如果您希望我添加解释,请告诉我您应该反转您的比较,即“WHERE ra.acctstarttime>DATE\u SUBnow,interval 3 month”@sqlab请查看问题中的更新。幸运的是,这没有给我任何结果。我确实有一条记录在上个月内没有开始日期,我已将日期子项改为显示1周,但该记录没有显示。@TheHumbleRat看不到您需要哪一行?抱歉,我已更正了我的问题,因为有一个类型ie本来就不需要。请查看我问题中的更新。@TheHumbleRat select date\u subcurrent\u date,间隔3个月;将是2014年6月26日。因此,任何在6月26日之后有记录的用户名都应该出现。除账单ID 24外,还应显示ID 13和26。是这样吗?不应该只有Bill和其他用户出现在表中,Steve和Fred的日期在过去三个月内。例如,Fred在2014年7月7日之后有一个条目,Steve在2014年7月11日之后也有一个条目。因此,在过去三个月内,这两个组织都很活跃。然而,比尔没有一个在过去三个月内的开始日期,因此这就是我需要看到的,那些在过去三个月内没有活动的人。@TheHumbleRat哦,你说得对!谢谢你的解释。让我看看我能做些什么我在这里添加了另一个查询。如果我能得到Bill的行,显示4月份的日期,那么我可以应用WHERE语句,我就会得到我需要的结果。
select t.*
from radacct t
join (
select ra.username, max(ra.acctstarttime) as acctstarttime
from radacct as ra
WHERE ra.acctstarttime < DATE_SUB(now(), interval 3 month)
) s on t.username = s.username and t.acctstarttime = s.acctstarttime
create table test
(
username varchar(20),
date_start date
);
Username date_start
--------- -----------
bill 2014-09-25
bill 2014-09-22
bill 2014-05-26
andy 2014-05-26
tim 2014-09-25
tim 2014-05-26
Username date_start
--------- -----------
bill 2014-09-25
tim 2014-09-25
select *
from test a
inner join
(
select username, max(date_start) as max_date_start
from test
where date_start > date_sub(now(), interval 3 month)
group by username
) b
on
a.username = b.username
and a.date_start = b.max_date_start
where
date_start > date_sub(now(), interval 3 month)
Username date_start
--------- -----------
bill 2014-05-26
tim 2014-05-26
andy 2014-05-26
select *
from test a
inner join
(
select username, max(date_start) as max_date_start
from test
where date_start < date_sub(now(), interval 3 month)
group by username
) b
on
a.username = b.username
and a.date_start = b.max_date_start
where
date_start < date_sub(now(), interval 3 month)
select *
from activity a
-- find max dates for users for records with dates after 3 months
inner join
(
select username, max(acctstarttime) as max_date_start
from activity
where acctstarttime < date_sub(now(), interval 3 month)
group by username
) b
on
a.username = b.username
and a.acctstarttime = b.max_date_start
-- find usernames who have data in the recent three months
left join
(
select username, count(*)
from activity
where acctstarttime >= date_sub(now(), interval 3 month)
group by username
) c
on
a.username = c.username
where
acctstarttime < date_sub(now(), interval 3 month)
-- choose users who DONT have data from recent 3 months
and c.username is null