【问题标题】:Inner join two tables with date results内部连接两个带有日期结果的表
【发布时间】:2023-03-21 05:16:01
【问题描述】:

结果应显示根据今天的日期,在过去 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 的结果

【问题讨论】:

  • 你的例子不清楚..
  • 今天几号?
  • @Strawberry 今天的日期总是今天,例如 2018-02-26 所以如果我明天运行报告应该是 2018-02-27
  • @scaisEdge 请告诉我要解释什么
  • 所以最终结果不一定是 253

标签: mysql select inner-join


【解决方案1】:

我想这就是你想要的:

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

【讨论】:

  • 如果客户没有签到会怎样?
  • 这个结果显示了很多行,我可以看到 client_id 出现了不止一次,例如 client_id 4 在结果中出现了 65 次,这是他签到的总数。在查询的哪一部分应该为每个 client_id 设置一个限制?
  • 好吧,从我现在的理解来看,它会返回从当前日期起签到超过 15 天的所有客户(每个 client_id 仅一次),但它也会返回例如昨天签到的客户。所以我需要排除那些我只想退回过去 15 天没有签到的客户。
  • @Andr 请立即尝试。我希望这次我做对了:)
  • where c.date &gt;= current_date() - INTERVAL 14 DAY 这样的东西可以使用索引
【解决方案2】:

我已经模拟了你的两个表如下:

mysql> select * from t03;
+------+-----------+---------------------+
| 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 |
+------+-----------+---------------------+
3 rows in set (0.00 sec)

mysql> select * from t04;
+------+-----------+---------------------+
| id   | client_id | date                |
+------+-----------+---------------------+
|    3 |       274 | 2018-02-25 15:01:09 |
|    2 |       265 | 2017-12-14 16:18:23 |
|    1 |       253 | 2018-01-18 13:18:21 |
+------+-----------+---------------------+

您的所有需求都如下代码。

select * from t04 where client_id in (select client_id from t03 where expiring > current_date()) and datediff(current_date() ,date) > 15  ;

然后你会得到如下结果:

+------+-----------+---------------------+
| id   | client_id | date                |
+------+-----------+---------------------+
|    1 |       253 | 2018-01-18 13:18:21 |
+------+-----------+---------------------+

【讨论】:

    【解决方案3】:

    给你。这是提取特定结果的查询

    select * from checkins t1 join subscription t2 on t1.client_id=t2.client_id where
    date(t2.expiring) >= date(NOW()) and datediff(current_date(), date(t1.date)) > 14;
    

    【讨论】:

    • 您需要为问题的“过去 15 天”部分添加测试。
    • 正是这个查询与 15 天没有任何关系
    • 嗨,问题不清楚,所以我很困惑。现在我已经更新了查询兄弟。谢谢你提醒我。
    • 谢谢,但返回有很多行,每个 client_id 出现不止一次,所以我如何限制每个 client_id 一次?
    • 您可以按client_id分组。对于每个客户 ID,您只会得到一个结果
    猜你喜欢
    • 2012-07-13
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多