0

登录/注销事件记录在如下表中:

user_id     object_id   date_event
7           2           2013-05-03 08:37:11
7           3           2013-05-03 12:43:51
7           2           2013-05-03 15:33:41
7           3           2013-05-03 17:10:01

记录了其他对象,但 2 和 3 是重要的(2=登录,3=注销)。

我遇到了一个查询,该查询将显示登录的总时间。我以此为起点,但我的查询返回的总登录时间为 null。

        SELECT 
            el.user_id, 
            TIME(SUM(el.date_event*(1-2*el.object_id))) AS total_time
        FROM 
            event_log el 
        WHERE 
            DATE(el.date_event) = '2013-05-03' AND 
            el.object_id IN (2,3) AND 
            el.user_id = 9
        GROUP BY 
            el.user_id;

这会产生:

user_id     total_time
9           NULL

我期望的地方

user_id     total_time
9           05:43:00

为什么它返回NULL?数据集中没有空值。

4

2 回答 2

2

假设 3 是注销,2 是登录

SELECT  T1.user_id, 
        T1.date_event,
        MIN(TimeDiff((T2.date_event), T1.date_event )) AS DaysDiff
FROM    YourTable T1
        LEFT JOIN YourTable T2
            ON T1.user_id = T2.user_id
            AND T2.date_event > T1.date_event
WHERE T1.object_id = 2 AND T2.object_id = 3
GROUP BY   T1.user_id, 
        T1.date_event
于 2013-05-03T22:03:42.777 回答
2

我有另一种方法

SET @given_user = 7;
SET @dt = '2013-05-03';
SET @dt1 = @dt + INTERVAL 0 SECOND;
SET @dt2 = @dt + INTERVAL 1 DAY;
SELECT
    SEC_TO_TIME(SUM(dts)) TotalTime
FROM
(
    SELECT
        (IF(object_id=2,-1,1) *
        (UNIX_TIMESTAMP(date_event) -
        UNIX_TIMESTAMP(DATE(date_event) +INTERVAL 0 SECOND))) dts
    FROM
        event_log
    WHERE
        user_id=@given_user AND
        object_id IN (2,3) AND
        date_event >= @dt1 AND
        date_event < @dt2
) A;

这是您的示例数据

use test
DROP TABLE IF EXISTS event_log;
CREATE TABLE event_log
(
    id INT NOT NULL AUTO_INCREMENT,
    user_id INT NOT NULL,
    object_id INT NOT NULL,
    date_event DATETIME,
    PRIMARY KEY (id)
);
INSERT into event_log
(user_id,object_id,date_event) VALUES
(7,2,'2013-05-03 08:37:11'),
(7,3,'2013-05-03 12:43:51'),
(7,2,'2013-05-03 15:33:41'),
(7,3,'2013-05-03 17:10:01');

在这里加载

use test
Database changed
mysql> DROP TABLE IF EXISTS event_log;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE event_log
    -> (
    ->     id INT NOT NULL AUTO_INCREMENT,
    ->     user_id INT NOT NULL,
    ->     object_id INT NOT NULL,
    ->     date_event DATETIME,
    ->     PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT into event_log
    -> (user_id,object_id,date_event) VALUES
    -> (7,2,'2013-05-03 08:37:11'),
    -> (7,3,'2013-05-03 12:43:51'),
    -> (7,2,'2013-05-03 15:33:41'),
    -> (7,3,'2013-05-03 17:10:01');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql>

这是执行的查询

mysql> SET @given_user = 7;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @dt = '2013-05-03';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @dt1 = @dt + INTERVAL 0 SECOND;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @dt2 = @dt + INTERVAL 1 DAY;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT
    ->     SEC_TO_TIME(SUM(dts)) TotalTime
    -> FROM
    -> (
    ->     SELECT
    ->         (IF(object_id=2,-1,1) *
    ->         (UNIX_TIMESTAMP(date_event) -
    ->         UNIX_TIMESTAMP(DATE(date_event) +INTERVAL 0 SECOND))) dts
    ->     FROM
    ->         event_log
    ->     WHERE
    ->         user_id=7 AND
    ->         object_id IN (2,3) AND
    ->         date_event >= @dt1 AND
    ->         date_event < @dt2
    -> ) A;
+-----------+
| TotalTime |
+-----------+
| 05:43:00  |
+-----------+
1 row in set (0.00 sec)

mysql>

试试看 !!!

于 2013-05-03T22:33:16.773 回答