SELECT
t3.id,
t3.prod_ID,
MIN(diff) AS min_time
FROM
(SELECT
t1.id,
(UNIX_TIMESTAMP(t2.time_stamp_2) - UNIX_TIMESTAMP(t1.time_stamp)) AS diff
FROM
production t1
LEFT JOIN
process t2 ON t1.id = t2.id
HAVING
diff >= 0) tx
LEFT JOIN
production t3 ON t3.id = tx.id
GROUP BY
t3.id
运行后,返回结果为:
id prod_ID min_time
-----------------------
1 2 1200
它应该返回的是
id prod_ID min_time
1 9 1200
最初以为加入有错误,经过多次加入测试,同样的错误结果。
SQLFiddle_2澄清我使用 Group By 因为我有多个 ID
SQLFiddle_3进行更多扩展。
在 SQLFiddle_3 上运行后,返回结果为:
id prod_ID min_time
-----------------------
1 2 1200
2 2 960
3 2 360
应该是什么
id prod_ID min_time
-----------------------
1 9 1200
2 2 960
3 3 360