使用条款在第二张表中插入

时间:2019-01-14 08:46:59

标签: mysql sql

“路线”表最多包含6条相同的路线,只希望保留最新的“文件日期”记录并将其复制到“路线2”表中

Heidi SQL

TRUNCATE TABLE temp.Routes2 
INSERT INTO temp.Routes2 
SELECT * 
from temp.Routes 
where temp.`File Date` = select max(t2.`File Date`) from temp.Routes as t2
LIMIT 100

获取SQL错误1064

  

好的,我只是想念语法了

非常感谢 西蒙

2 个答案:

答案 0 :(得分:2)

您希望括号和外部查询的引用使其与子查询相关:

SELECT t.*
FROM temp.Routes as t1
WHERE temp.`File Date` = (select max(t2.`File Date`) 
                          from temp.Routes as t2
                          where t1.col = t2.col
                         );

答案 1 :(得分:0)

尝试以下-

    INSERT INTO temp.Routes2 
    SELECT * 
    from temp.Routes a
    where a.`File Date` = (select max(t2.`File Date`) from temp.Routes as t2)
    order by temp.`File Date` limit 100