MySql 从其他 table 中删除

MySql delete from other table

我有两张桌子

表 1:

pnr, dmax
01,  2017-02-02 11:10:00
02,  2017-05-02 10:10:10

和表 2:

pnr, type, loc, dt
01,  c3,   l2,  2017-02-02 11:10:00
01,  c3,   l2,  2017-01-01 09:00:00
01,  c3,   l3,  2017-01-01 07:54:30
02,  c5,   l1,  2017-02-05 01:10:00
02,  c5,   l2,  2017-03-01 19:00:10
02,  c5,   l3,  2017-05-02 10:10:10

我想删除表 2 中没有在表 1 中找到的所有行,但我受限于我有限的 mysql 知识

类似于 ..... 其中 pnr = pnr AND dmax != dt

请帮忙

DELETE  table2
FROM    table2
        LEFT JOIN table1
            ON table1.pnr = table2.pnr AND table1.dmax = table2.dt
WHERE   table1.pnr IS NULL

上面的语句使用了基本的 LEFT JOIN。这将在右侧 return NULL table 左侧没有找到记录,这些是我们要删除的记录,因此我们将 IS NULL 添加到 where 子句

您需要: DELETE FROM table2 WHERE NOT EXISTS (SELECT a.pnr, a.dmax FROM table1 a WHERE a.pnr = table2.pnr AND a.dmax = table2.dt)