首页 > 解决方案 > 在sql上写查询

问题描述

我有一张monitoring带有客户 ID 列的表ID,即应用程序的最后登录日期Time。我写了一个查询来显示客户在什么时候可以访问系统的表格:时间-此时的条目数-客户ID

要求:

select Time, count (*) as Quantity, group_concat (ClientID) from monitoring group by Time;

如何编写以下查询?以相同的形式显示表格,只是现在需要在每次至少有 1 个客户端访问时,显示当时没有访问权限的所有客户端的 id。

UPD。

+---------------------+-------------+----------------+
| Time                | Quantity    | ClientID       |                                                                                               
+---------------------+-------------+----------------+                                                                                                             
| 2018-06-14 15:51:03 |       3     | 311,240,528    |                                                                                                    
| 2018-06-14 15:51:20 |       3     | 314,312,519    |                                                                                                    
| 2019-01-14 06:00:07 |       1     | 359            |                                                                                                    
| 2019-08-21 14:30:04 |       1     | 269            |                                                                                                    
+---------------------+-------------+----------------+

这些是当前有权访问的客户的 ID。并且您需要显示在该特定时间没有访问权限的所有客户端的 ID 即,在这种情况下:

+---------------------+-------------+-----------------------------+
| Time                | Quantity    | ClientID                    |                                                                                               
+---------------------+-------------+-----------------------------+                                                                                                            
| 2018-06-14 15:51:03 |           5 | 269,359,314,312,519         |                                                                                                    
| 2018-06-14 15:51:20 |           5 | 311,240,528,359,269         |                                                                                                    
| 2019-01-14 06:00:07 |           7 | 311,240,528,314,312,519,269 |                                                                                                    
| 2019-08-21 14:30:04 |           7 | 311,240,528,314,312,519,359 |                                                                                                    
+---------------------+-------------+-----------------------------+

建议不要考虑日期和时间,而只考虑年和月。但一出来。谢谢。

标签: mysqlsqldatetimecountsubquery

解决方案


cross join您可以使用两个子查询中的 a 生成所有可能的客户端和时间组合select distinct,然后使用 过滤掉表中存在的那些not exists。最后一步是聚合:

select t.time, count(*) as quantity, group_concat(c.clientid) as clientids
from (select distinct time from monitoring) t
cross join (select distinct clientid from monitoring) c
where not exists (
    select 1
    from monitoring m
    where m.time = t.time and m.clientid = c.clientid
)
group by t.time

我不清楚问题中最后一句话是什么意思。上述查询将生成您为示例数据显示的结果。


推荐阅读