2

我在 MySQL 中有五个表,我从中生成一些报告。出于显示目的,我将稍微抽象一下表名。假设我正在使用的表如下所述:

History  (id, serial, date, time) : Stores historic data, this is the main
                                    column we query.
Serial   (id, serial)             : Stores serial numbers used in the next
                                    3 tables.
Employee (id, serial_id, name)
Event    (id, serial_id, name)
Location (id, serial_id, name)

编辑:重写了整个问题,希望它更清晰,更容易理解......

我需要查询事件,如果在历史记录中直接在与事件匹配的行之前找到员工序列,则员工将与该事件相关联,同样,如果在历史记录中直接找到位置之后与事件匹配的序列的行,该位置将与该事件相关联。

样本数据:

History:
+------+------------+------------+----------+
| id   | serial     | date       | time     |
+------+------------+------------+----------+
| 1    | 00001234   | 2012-09-25 | 09:00:00 |
+------+------------+------------+----------+
| 2    | 00001235   | 2012-09-25 | 09:01:00 |
+------+------------+------------+----------+
| 3    | 00001236   | 2012-09-25 | 09:02:00 |
+------+------------+------------+----------+
| 4    | 00001235   | 2012-09-25 | 09:05:00 |
+------+------------+------------+----------+
| 5    | 00001236   | 2012-09-25 | 09:07:00 |
+------+------------+------------+----------+
| 6    | 00001235   | 2012-09-25 | 09:10:00 |
+------+------------+------------+----------+
| 7    | 00001235   | 2012-09-25 | 09:11:00 |
+------+------------+------------+----------+
| 8    | 00001235   | 2012-09-25 | 09:12:00 |
+------+------------+------------+----------+
| 9    | 00001236   | 2012-09-25 | 09:15:00 |
+------+------------+------------+----------+
| 10   | 00001234   | 2012-09-25 | 09:20:00 |
+------+------------+------------+----------+
| 11   | 00001236   | 2012-09-25 | 09:25:00 |
+------+------------+------------+----------+

Serial:
+----------+------------+
| id       | serial     |
+----------+------------+
| 1        | 00001234   |
+----------+------------+
| 2        | 00001235   |
+----------+------------+
| 3        | 00001236   |
+----------+------------+

Employee:
+----------+------------+-------------+
| id       | serial_id  | name        |
+----------+------------+-------------+
| 1        | 1          | John Smith  |
+----------+------------+-------------+

Event:
+----------+------------+-------------+
| id       | serial_id  | name        |
+----------+------------+-------------+
| 1        | 2          | Event 1     |
+----------+------------+-------------+

Location:
+----------+------------+-------------+
| id       | serial_id  | name        |
+----------+------------+-------------+
| 1        | 3          | Location 1  |
+----------+------------+-------------+

如果我要使用以下查询进行普通的 LEFT JOIN 查询(​​只是为了清楚起见作为结果的差异),结果将是:

SELECT
    history.id,
    history.serial,
    history.date,
    history.time,
    employee.name as 'employee',
    event.name as 'event',
    location.name as 'location'
FROM history
LEFT JOIN serial ON history.serial = serial.serial
LEFT JOIN employee ON serial.id = employee.serial_id
LEFT JOIN event ON serial.id = event.serial_id
LEFT JOIN location ON location.id = location.serial_id
ORDER BY date, time

正常 LEFT JOIN 的结果是(为了清楚起见,我不需要这个结果集):

+----+----------+------------+----------+------------+----------+------------+
| id | serial   | date       | time     | employee   | event    | location   |
+----+----------+------------+----------+------------+----------+------------+
| 1  | 00001234 | 2012-09-25 | 09:00:00 | John Smith | NULL     | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 2  | 00001235 | 2012-09-25 | 09:01:00 | NULL       | Event 1  | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 3  | 00001236 | 2012-09-25 | 09:02:00 | NULL       | NULL     | Location 1 |
+----+----------+------------+----------+------------+----------+------------+
| 4  | 00001235 | 2012-09-25 | 09:05:00 | NULL       | Event 1  | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 5  | 00001236 | 2012-09-25 | 09:07:00 | NULL       | NULL     | Location 1 |
+----+----------+------------+----------+------------+----------+------------+
| 6  | 00001235 | 2012-09-25 | 09:10:00 | NULL       | Event 1  | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 7  | 00001235 | 2012-09-25 | 09:11:00 | NULL       | Event 1  | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 8  | 00001235 | 2012-09-25 | 09:12:00 | NULL       | Event 1  | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 9  | 00001236 | 2012-09-25 | 09:15:00 | NULL       | NULL     | Location 1 |
+-----+---------+------------+----------+------------+----------+------------+
| 10 | 00001234 | 2012-09-25 | 09:20:00 | John Smith | NULL     | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 11 | 00001236 | 2012-09-25 | 09:25:00 | NULL       | NULL     | Location 1 |
+----+----------+------------+----------+------------+----------+------------+

查询应该基于事件,事件永远不应该为 NULL。它应该查看PRIOR行并将其标记为员工(如果不匹配,则为 null),它应该查看AFTER行并将其标记为位置(如果不匹配,则为 null)。使用上述数据的结果示例:

Result should be:
+----------+------------+------------+------------+----------+------------+
| e_serial | date       | time       | employee   | event    | location   |
+----------+------------+------------+------------+----------+------------+
| 00001235 | 2012-09-25 | 09:01:00   | John Smith | Event 1  | Location 1 |
+----------+------------+------------+------------+----------+------------+
| 00001235 | 2012-09-25 | 09:05:00   | NULL       | Event 1  | Location 1 |
+----------+------------+------------+------------+----------+------------+
| 00001235 | 2012-09-25 | 09:10:00   | NULL       | Event 1  | NULL       |
+----------+------------+------------+------------+----------+------------+
| 00001235 | 2012-09-25 | 09:11:00   | NULL       | Event 1  | NULL       |
+----------+------------+------------+------------+----------+------------+
| 00001235 | 2012-09-25 | 09:12:00   | NULL       | Event 1  | Location 1 |
+----------+------------+------------+------------+----------+------------+

我不确定这是否可以通过一些子选择或其他东西来实现,我认为这已经超出了我与 SQL 的联盟。目前我正在使用应用程序逻辑,但认为仅使用 SQL 处理它可能会更干净、更快捷。

任何帮助都会很棒。

4

2 回答 2

2
SELECT serial_id AS `serial`,
(SELECT `date` FROM History WHERE serial_id= event2.serial_id ) AS `date`,
(SELECT `time` FROM History WHERE serial_id= event2.serial_id ) AS `time`,
(SELECT `name` FROM event1 WHERE serial_id= event2.serial_id ) AS `event1`,`name` AS event2,
(SELECT `name` FROM event3 WHERE serial_id= event2.serial_id ) AS `event1`
FROM event2

你的历史表应该是History (id, serial_id, date, time),必须包含serial_id而不是serial name

对于您的情况,希望它可以正常工作

SELECT serial_id AS `serial`,
(SELECT `date` FROM History WHERE `serial` = (SELECT `serial` FROM `serial` WHERE id = event2.serial_id LIMIT 0,1)) AS `date`,
(SELECT `time` FROM History WHERE `serial`= (SELECT `serial` FROM `serial` WHERE id = event2.serial_id LIMIT 0,1)) AS `time`,
(SELECT `name` FROM event1 WHERE serial_id= event2.serial_id ) AS `event1`,
`name` AS event2,
(SELECT `name` FROM event3 WHERE serial_id= event2.serial_id ) AS `event1`
FROM event2
于 2012-09-25T05:28:58.193 回答
2

如果其他人遇到问题,请在此处发布解决方案。感谢 dianuj 引导我朝着正确的方向前进。

SELECT
    serial.serial as 'e_serial',
    @dt:=TIMESTAMP(history.date, history.time) as 'date_time',
    history.date,
    history.time,
    (SELECT
        employee.name
    FROM
        history
        LEFT JOIN serial ON history.serial = serial.serial
        LEFT JOIN employee ON employee.serial_id = serial.id
    WHERE
        TIMESTAMP(history.date, history.time) = (
            SELECT
                max(TIMESTAMP(history.date, history.time))
            FROM
                history
            WHERE TIMESTAMP(history.date, history.time) < @dt
        )
    ) as 'employee',
    event.name as 'event',
    (SELECT
        location.name
    FROM
        history
        LEFT JOIN serial ON history.serial = serial.serial
        LEFT JOIN location ON location.serial_id = serial.id
    WHERE
        TIMESTAMP(history.date, history.time) > @dt
    LIMIT 1
    ) as 'location'
FROM history
LEFT JOIN serial ON history.serial = serial.serial
LEFT JOIN event ON serial.id = event.serial_id
WHERE event.name IS NOT NULL
ORDER BY history.date, history.time

和结果

+----------+---------------------+------------+----------+------------+---------+------------+
| e_serial | date_time           | date       | time     | employee   | event   | location   |
+----------+---------------------+------------+----------+------------+---------+------------+
| 00001235 | 2012-09-25 09:01:00 | 2012-09-25 | 09:01:00 | John Smith | Event 1 | Location 1 |
| 00001235 | 2012-09-25 09:05:00 | 2012-09-25 | 09:05:00 | NULL       | Event 1 | Location 1 |
| 00001235 | 2012-09-25 09:10:00 | 2012-09-25 | 09:10:00 | NULL       | Event 1 | NULL       |
| 00001235 | 2012-09-25 09:11:00 | 2012-09-25 | 09:11:00 | NULL       | Event 1 | NULL       |
| 00001235 | 2012-09-25 09:12:00 | 2012-09-25 | 09:12:00 | NULL       | Event 1 | Location 1 |
+----------+---------------------+------------+----------+------------+---------+------------+
5 rows in set (0.01 sec)
于 2012-09-26T01:12:56.353 回答