我在 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 处理它可能会更干净、更快捷。
任何帮助都会很棒。