gpt4 book ai didi

mysql - 内连接两个带有日期结果的表

转载 作者:行者123 更新时间:2023-11-29 17:54:31 24 4
gpt4 key购买 nike

结果应该显示根据今天的日期,过去 15 天内哪些活跃客户没有访问过该地点。

我有两个表是:

**Table: subscription**
------------------------------------
id | client_id | expiring
------------------------------------
1 | 253 | 2018-03-22 00:00:00
2 | 265 | 2018-02-14 00:00:00
3 | 274 | 2018-05-29 00:00:00
------------------------------------

**Table: checkins**
------------------------------------
id | client_id | date
------------------------------------
1 | 253 | 2018-01-18 13:18:21
2 | 265 | 2017-12-14 16:18:23
3 | 274 | 2018-02-25 15:01:09
------------------------------------

所以最终结果应该显示 1 个 client_id 253 的结果

最佳答案

我想这就是你想要的:

select distinct s.client_id from subscription s
where s.expiring >= current_date()
and not exists (
select 1 from checkins c
where datediff(current_date(), c.date) <= 14
and c.client_id = s.client_id
)

where 子句中的第一个条件将结果限制为活跃客户端,第二个条件排除过去 14 天内访问过的客户端。

Sample SQL Fiddle

关于mysql - 内连接两个带有日期结果的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48984838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com