我在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)
编辑:重写整个问题,希望它更清晰,更容易理解......
我需要查询事件,如果在历史记录中直接找到员工序列 PRIOR 到具有与事件匹配的序列的行,该员工将与该事件相关联,同样,如果在历史记录中直接找到一个位置 AFTER 具有与事件匹配的序列的行,该位置将与该事件相关联。
示例数据:
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 并将其标记为employee(如果不匹配则为null),它应该查看 AFTER 行并将其标记为位置(如果为没有比赛)。结果应该是什么样的例子,使用上面的数据:
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处理它可能更干净,更快。
任何帮助都会很棒。
答案 0 :(得分:1)
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
答案 1 :(得分:1)
如果其他人遇到此问题,请在此处发布解决方案。感谢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)