以下是包含子查询的SQL查询。基本上我希望在给定的月份和年份获得我们设备的最新仪表读数。我知道这是在我的子查询中,问题出现了,但我不知道如何正确解决它。
如果我删除子查询并且只选择当月的仪表读数设备,则会产生以下结果。
eid eqid name pid hours date
70 C1 KOMATSU WA250 3YD BUCKET 27 1176 2013-10-07
70 C1 KOMATSU WA250 3YD BUCKET 27 1195 2013-10-28
70 C1 KOMATSU WA250 3YD BUCKET 27 1178 2013-10-14
73 C11 KOMATSU PC200 EXCAVATOR 27 1080 2013-10-14
73 C11 KOMATSU PC200 EXCAVATOR 27 1099 2013-10-28
73 C11 KOMATSU PC200 EXCAVATOR 27 1078 2013-10-07
92 C4 CATERPILLAR 304D MINI EX 27 646 2013-10-14
92 C4 CATERPILLAR 304D MINI EX 27 645 2013-10-07
92 C4 CATERPILLAR 304D MINI EX 27 649 2013-10-28
58 E14 BOBCAT-ATV 2300 Utility 8 522 2013-10-31
61 E17 SKYTRAK FORKLIFT 40 943 2013-10-10
62 E18 SKYTRAK FORKLIFT 5 1790 2013-10-30
62 E18 SKYTRAK FORKLIFT 5 1789 2013-10-29
62 E18 SKYTRAK FORKLIFT 5 1777 2013-10-13
62 E18 SKYTRAK FORKLIFT 5 1772 2013-10-07
62 E18 SKYTRAK FORKLIFT 5 1777 2013-10-13
62 E18 SKYTRAK FORKLIFT 5 1772 2013-10-04
62 E18 SKYTRAK FORKLIFT 5 1772 2013-10-07
62 E18 SKYTRAK FORKLIFT 5 1772 2013-10-04
67 E23 BOBCAT SKID STEER 27 1178 2013-10-28
以下是完整查询的结果,包括子查询。
92 C4 CATERPILLAR 304D MINI EX 27 649 2013-10-28
61 E17 SKYTRAK FORKLIFT 40 943 2013-10-10
62 E18 SKYTRAK FORKLIFT 5 1790 2013-10-30
以下是我正在使用的查询。
SELECT e.eid, e.eqid, e.name, m.pid, m.hours, m.date FROM meter m
JOIN equipment e ON m.eid = e.eid
WHERE MONTH(date) = $month
AND YEAR(date) = $year
AND m.date = (SELECT MAX(m2.date) FROM meter m2 WHERE m2.eid = m.eid)
ORDER BY e.eqid ASC
非常感谢任何帮助。
EDIT *** 我本来就没有塞巴斯。我不得不改变一件事而且效果很好。
SELECT DISTINCT e.eid, e.eqid, e.name, m.pid, m.hours, m.date
FROM equipment e
JOIN (
SELECT eid, MAX(date) as date
FROM meter
WHERE MONTH(date) = $month
AND YEAR(date) = $year
GROUP BY eid
) maxdate ON maxdate.eid = e.eid
JOIN meter m ON m.eid = e.eid AND m.date = maxdate.date
ORDER BY e.eqid ASC
答案 0 :(得分:1)
猜出每个表的字段,我想出了以下查询:
SELECT DISTINCT e.eid, e.eqid, e.name, m.pid, m.hours, m.date
FROM
equipment e
JOIN (
SELECT eid, MAX(date) date
FROM meter
WHERE
MONTH(date) = $month
AND YEAR(date) = $year
GROUP BY eid
) maxdate ON maxdate.eid = e.eid
JOIN meter m ON m.eid = e.eid AND m.date = maxdate.date
ORDER BY e.eqid ASC
答案 1 :(得分:1)
尝试
SELECT e.eid, e.eqid, e.name, m.pid, m.hours, m.date
FROM
(
SELECT eid, MAX(date) date
FROM meter
WHERE date BETWEEN '2013-10-01' AND LAST_DAY('2013-10-01')
GROUP BY eid
) q JOIN meter m
ON q.eid = m.eid
AND q.date = m.date JOIN equipment e
ON q.eid = e.eid
ORDER BY e.eid
注意:确保您在date
上有索引。不要将任何函数(MONTH()
等)应用于WHERE
子句中的这些列,因为它会阻止您使用任何可能导致全部表扫描的索引。
示例输出:
| EID | EQID | NAME | PID | HOURS | DATE | |-----|------|--------------------------|-----|-------|------------| | 58 | E14 | BOBCAT-ATV 2300 Utility | 8 | 522 | 2013-10-31 | | 61 | E17 | SKYTRAK FORKLIFT | 40 | 943 | 2013-10-10 | | 62 | E18 | SKYTRAK FORKLIFT | 5 | 1790 | 2013-10-30 | | 67 | E23 | BOBCAT SKID STEER | 27 | 1178 | 2013-10-28 | | 70 | C1 | KOMATSU WA250 3YD BUCKET | 27 | 1195 | 2013-10-28 | | 73 | C11 | KOMATSU PC200 EXCAVATOR | 27 | 1099 | 2013-10-28 | | 92 | C4 | CATERPILLAR 304D MINI EX | 27 | 649 | 2013-10-28 |
这是 SQLFiddle 演示