Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
请有人帮助我,我有一个用户表和另一个包含用户和日期时间的表(这是一个日志文件,每个用户存在多个日期)。我需要从日志表中获取最近的日期并将其插入到同一用户旁边的第一个表中。
这就是我所拥有的,但它不起作用:
INSERT INTO tb1 n (DT) SELECT w.DT FROM tb2 w WHERE w.User = n.User ORDER BY w.DT DESC limit 1
您不需要INSERT在此处使用语句,因为您的表上已经存在记录。但取而代之UPDATE的是JOIN
INSERT
UPDATE
JOIN
UPDATE tb1 a INNER JOIN ( SELECT user, MAX(DT) maxDT FROM tb2 GROUP by user ) b ON a.user = b.user SET a.DT = b.maxDT