select * from automated_task_center_test_wxj.atc_machine_heartbeat_logs t1 join (selectmax(heartbeat) a, machine_id b from automated_task_center_test_wxj.atc_machine_heartbeat_logs groupby b) t on t1.heartbeat=t.a and t1.machine_id=t.b orderby machine_id
1 2 3 4 5 6 7 8
--标准答案之inner join的另一种写法 和innerjoin语义上是等价
select * from automated_task_center_test_wxj.atc_machine_heartbeat_logs t1, (selectmax(heartbeat) a, machine_id b from automated_task_center_test_wxj.atc_machine_heartbeat_logs groupby b) t where t1.heartbeat=t.a and t1.machine_id=t.b orderby machine_id
插播1
为什么等价? 请教inner join 与一般笛卡尔积的区别,如select * from tb1 a inner join tb2 b on a.id=b.id 与select * from tb1 a ,tb2 b where a.id=b.id 有什么不同?谢谢! 答案:等价
SELECT * FROM automated_task_center_test_wxj.atc_machine_heartbeat_logs WHEREidin (SELECTMAX(id) FROM automated_task_center_test_wxj.atc_machine_heartbeat_logs GROUPBY machine_id)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
--标准答案之left join 不咋快
SELECT a.* FROM automated_task_center_test_wxj.atc_machine_heartbeat_logs a LEFTJOIN automated_task_center_test_wxj.atc_machine_heartbeat_logs b ON a.machine_id = b.machine_id AND a.heartbeat < b.heartbeat WHERE b.machine_id ISNULL;
-- 为什么left join这么写是对的?How it works: -- It matches each row from o with all the rows from b having the same value in column Group and a bigger value in column Age. Any row from o not having the maximum value of its group in column Age will match one or more rows from b.
-- The LEFT JOIN makes it match the oldest person in group (including the persons that are alone in their group) with a row full of NULLs from b ('no biggest age in the group'). -- Using INNER JOIN makes these rows not matching and they are ignored.
-- The WHERE clause keeps only the rows having NULLs in the fields extracted from b. They are the oldest persons from each group.
1 2 3 4
-- 这个写法貌似也对?我理解不了这个写法 貌似和经典inner等价 https://stackoverflow.com/questions/8470311/get-the-latest-date-from-grouped-mysql-data SELECT d.machine_id, d.heartbeat, d.cpu, d.memory FROM automated_task_center_test_wxj.atc_machine_heartbeat_logs d WHERE d.heartbeat IN (SELECTmax(d2.heartbeat) FROM automated_task_center_test_wxj.atc_machine_heartbeat_logs d2 WHERE d2.machine_id=d.machine_id)
1 2 3 4 5 6
--理解不了的写法二 和上面有点像 来自思否中Ewellyuan被质疑的回答 https://segmentfault.com/q/1010000004138670 select * from automated_task_center_test_wxj.atc_machine_heartbeat_logs as u where u.heartbeat = (selectmax(heartbeat) from automated_task_center_test_wxj.atc_machine_heartbeat_logs where machine_id = u.machine_id)