我有一个有趣的。我要加入 3 张桌子。我加入的最后一张表,我只想按日期查看最新条目,而不是用同一个用户将我的结果加倍,只是有不同的登录时间。
示例(这只是一个示例表):
SELECT a.user_id
a.user_name,
b.department,
c.last_logon_date_time,
c.computer_name
FROM table1 a
LEFT OUTER JOIN table2 b ON a.user_id = b.user_id
LEFT OUTER JOIN table3 c ON c.user_id = c.user_id
下面会给我我正在寻找的结果,但看起来效率很低,而且速度很慢。有什么方法可以加快速度并提高效率吗?
我可以这样做
SELECT a.user_id
a.user_name,
b.department,
(SELECT c.last_logon_datetime FROM table 3 c WHERE a.user_id = c.user_id ORDER BY c.last_logon_datetime DESC LIMIT 1) as last_logon_datetime,
(SELECT c.computer_name FROM table 3 c WHERE a.user_id = c.user_id ORDER BY c.last_logon_datetime DESC LIMIT 1) as computer_name
FROM table1 a
LEFT OUTER JOIN table2 b ON a.user_id = b.user_id
谢谢你。