根据参数联接两个表

时间:2019-02-15 16:27:01

标签: mysql

我需要计算用户在其进入网站的ENTERS之间产生的要约视图。

我有两个表:

ENTERS
+----+---------------------+---------+
| id |     action_date     | user_id |
+----+---------------------+---------+
|  1 | 2019-02-14 05:59:09 |  263124 |
|  2 | 2019-02-14 06:56:25 |  263124 |
|  3 | 2019-02-14 09:10:45 |  263124 |
|  4 | 2019-02-14 19:42:44 |  263124 |
|  5 | 2019-02-14 20:00:35 |  263124 |
|  6 | 2019-02-14 07:40:27 |  501064 |
|  8 | 2019-02-14 10:43:49 |  501064 |
|  9 | 2019-02-14 11:51:17 |  501064 |
| 11 | 2019-02-14 15:20:34 |  501064 |
| 12 | 2019-02-14 15:45:03 |  596244 |
| 13 | 2019-02-14 16:18:33 |  596244 |
| 14 | 2019-02-14 17:25:03 |  596244 |
| 15 | 2019-02-14 19:47:02 |  596244 |
+----+---------------------+---------+

VIEWS
+----+---------------------+---------+
| id |        data         | user_id |
+----+---------------------+---------+
|  1 | 2019-02-14 06:59:15 |  263124 |
|  2 | 2019-02-14 07:01:18 |  263124 |
|  3 | 2019-02-14 18:36:26 |  263124 |
|  4 | 2019-02-14 19:42:57 |  263124 |
|  5 | 2019-02-14 19:43:19 |  263124 |
|  6 | 2019-02-14 19:49:36 |  263124 |
|  7 | 2019-02-14 20:00:59 |  263124 |
|  8 | 2019-02-14 20:02:54 |  263124 |
|  9 | 2019-02-14 20:23:08 |  263124 |
| 10 | 2019-02-14 11:00:24 |  501064 |
| 11 | 2019-02-14 11:08:29 |  501064 |
| 12 | 2019-02-14 22:22:33 |  501064 |
| 13 | 2019-02-14 22:24:12 |  501064 |
| 14 | 2019-02-14 13:06:54 |  596244 |
| 15 | 2019-02-14 13:13:10 |  596244 |
| 16 | 2019-02-14 13:16:53 |  596244 |
| 17 | 2019-02-14 13:20:34 |  596244 |
| 18 | 2019-02-14 13:25:43 |  596244 |
| 19 | 2019-02-14 13:27:37 |  596244 |
| 20 | 2019-02-14 13:31:29 |  596244 |
| 21 | 2019-02-14 13:32:54 |  596244 |
| 22 | 2019-02-14 13:33:17 |  596244 |
| 23 | 2019-02-14 13:33:32 |  596244 |
| 24 | 2019-02-14 13:33:48 |  596244 |
| 25 | 2019-02-14 13:34:17 |  596244 |
| 26 | 2019-02-14 13:34:36 |  596244 |
| 27 | 2019-02-14 13:40:01 |  596244 |
| 28 | 2019-02-14 13:45:48 |  596244 |
| 29 | 2019-02-14 13:46:35 |  596244 |
| 30 | 2019-02-14 13:47:52 |  596244 |
| 31 | 2019-02-14 13:49:55 |  596244 |
| 32 | 2019-02-14 15:59:38 |  596244 |
| 33 | 2019-02-14 17:36:35 |  596244 |
| 34 | 2019-02-14 17:48:21 |  596244 |
| 35 | 2019-02-14 20:03:48 |  596244 |
+----+---------------------+---------+

我对mySQL 5.7.20数据库具有只读访问权限。 我认为我必须以某种方式使用参数,但我不知道如何执行此操作。

代码应该是这样的:

SELECT
t1.id,
t1.datetime,
t1.source,
t1.user_id,
COUNT(t2.id)

FROM
enters t1

LEFT JOIN views t2 ON t1.user_id = t2.user_id

WHERE
DATE(t1.datetime) BETWEEN '2019-01-01' AND '2019-01-02'
AND t2.datetime BETWEEN @t1.datetime and @t1.datetime_next

GROUP BY
t1.enter_id

我已设法通过以下公式在Excel中获得结果(也许它将有助于理解我要实现的目标):

=COUNTIFS(views!C:C;C14;views!B:B;">="&B14;views!B:B;"<"&mwids!B15)

但是该公式不是100%正确的,因为当列中的用户发生更改时,该公式会出现问题

预期结果: (注意:COUNT(views)基于Excel公式,因此在某些情况下可能会关闭,但它显示了我想要实现的目标)

+----+---------------------+---------+--------------+
| id |     action_date     | user_id | COUNT(views) |
+----+---------------------+---------+--------------+
|  1 | 2019-02-14 05:59:09 |  263124 |            0 |
|  2 | 2019-02-14 06:56:25 |  263124 |            2 |
|  3 | 2019-02-14 09:10:45 |  263124 |            1 |
|  4 | 2019-02-14 19:42:44 |  263124 |            3 |
|  5 | 2019-02-14 20:00:35 |  263124 |            0 |
|  6 | 2019-02-14 07:40:27 |  501064 |            0 |
|  8 | 2019-02-14 10:43:49 |  501064 |            2 |
|  9 | 2019-02-14 11:51:17 |  501064 |            0 |
| 11 | 2019-02-14 15:20:34 |  501064 |            0 |
| 12 | 2019-02-14 15:45:03 |  596244 |            1 |
| 13 | 2019-02-14 16:18:33 |  596244 |            0 |
| 14 | 2019-02-14 17:25:03 |  596244 |            2 |
| 15 | 2019-02-14 19:47:02 |  596244 |            0 |
+----+---------------------+---------+--------------+

1 个答案:

答案 0 :(得分:0)

一个问题是您输入日期但没有请假日期。您可以使用相关的子查询来解决此问题

select e.*
        ,date_sub(
        coalesce(
        (select action_date from enters e1 where e1.user_id = e.user_id and e1.action_date > e.action_date limit 1)
        ,now()
        ) 
        ,interval 1 second) leavedate
from enters e;

+------+---------------------+---------+---------------------+
| id   | action_date         | user_id | leavedate           |
+------+---------------------+---------+---------------------+
|    1 | 2019-02-14 05:59:09 |  263124 | 2019-02-14 06:56:24 |
|    2 | 2019-02-14 06:56:25 |  263124 | 2019-02-14 09:10:44 |
|    3 | 2019-02-14 09:10:45 |  263124 | 2019-02-14 19:42:43 |
|    4 | 2019-02-14 19:42:44 |  263124 | 2019-02-14 20:00:34 |
|    5 | 2019-02-14 20:00:35 |  263124 | 2019-02-16 09:26:18 |
|    6 | 2019-02-14 07:40:27 |  501064 | 2019-02-14 10:43:48 |
|    8 | 2019-02-14 10:43:49 |  501064 | 2019-02-14 11:51:16 |
|    9 | 2019-02-14 11:51:17 |  501064 | 2019-02-14 15:20:33 |
|   11 | 2019-02-14 15:20:34 |  501064 | 2019-02-16 09:26:18 |
|   12 | 2019-02-14 15:45:03 |  596244 | 2019-02-14 16:18:32 |
|   13 | 2019-02-14 16:18:33 |  596244 | 2019-02-14 17:25:02 |
|   14 | 2019-02-14 17:25:03 |  596244 | 2019-02-14 19:47:01 |
|   15 | 2019-02-14 19:47:02 |  596244 | 2019-02-16 09:26:18 |
+------+---------------------+---------+---------------------+
13 rows in set (0.00 sec)

并在汇总查询中使用它

select v.user_id,e.action_date,count(data)
from enters e
join views v on v.user_id = e.user_id
where v.data between e.action_date and
        date_sub(
        coalesce(
        (select action_date from enters e1 where e1.user_id = e.user_id and e1.action_date > e.action_date limit 1)
        ,now()
        ) 
        ,interval 1 second)
group by v.user_id,e.action_date;

+---------+---------------------+-------------+
| user_id | action_date         | count(data) |
+---------+---------------------+-------------+
|  263124 | 2019-02-14 06:56:25 |           2 |
|  263124 | 2019-02-14 09:10:45 |           1 |
|  263124 | 2019-02-14 19:42:44 |           3 |
|  263124 | 2019-02-14 20:00:35 |           3 |
|  501064 | 2019-02-14 10:43:49 |           2 |
|  501064 | 2019-02-14 15:20:34 |           2 |
+---------+---------------------+-------------+
6 rows in set (0.00 sec)

我注意到596244已完全退出-根据数据,这似乎是正确的,因为最早的输入日期晚于最早的查看日期。