mysql - 在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 |
+---------------------+-------------+-----------------------------+
建议不要考虑日期和时间,而只考虑年和月。但一出来。谢谢。
解决方案
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
我不清楚问题中最后一句话是什么意思。上述查询将生成您为示例数据显示的结果。
推荐阅读
- ios - Ionic App 在 Safari 浏览器或真实设备 (Cordova) 中不起作用
- ios - 应用程序在后台时来自 iOS 推送通知操作的 Http 请求
- xcode - 撤销钥匙串中的所有证书后,命令 PhaseScriptExecution 以非零退出代码失败
- android - 未找到 Android Studio 4.0.1 默认活动
- c# - 外部组件抛出异常
- matlab - 在 Simulink 中查找和替换
- c# - 如何在 C# 中使用 Selenium 在 aria-hidden=“true” 的元素上发送密钥
- javascript - 什么!!在 JS 中做什么?
- rest - IErrorHandler 中的响应内容类型错误
- matlab - 如何使用for循环(matlab)对n个数字(pcolor)进行子图绘制