MySQL Select 表 01 中不存在于表 02 中的行

MySQL Select rows that from table01 that doesn't exist on table02

我有两个表,table03 有 10 行,table01 有 21 行,现在我想从 table03 中获取 table01 中不存在的行,到目前为止我写了这个查询,但它显示了 table03 的所有行甚至有些行在 table01 上不存在。

SELECT T3.`DateAdded`, T3.`Query_name`, T3.`Fund`, T3.`Ticker` 
FROM `table_name03` T3 LEFT JOIN `table_name01` T1 
ON T3.`DateAdded` = T1.`DateAdded` 
AND `T3`.`Query_name` = `T1`.`Query_name` 
AND `T3`.`Fund` = `T1`.`Fund`
AND `T3`.`Ticker` = `T1`.`Ticker`

表03:

表01:

你走在正确的轨道上。您可以只添加一个 where 条件来过滤不匹配的行:

SELECT T3.DateAdded, T3.Query_name, T3.Fund, T3.Ticker 
FROM table_name03 T3 
LEFT JOIN table_name01 T1 
    ON  T3.DateAdded = T1.DateAdded 
    AND T3.Query_name = T1.Query_name 
    AND T3.Fund = T1.Fund
    AND T3.Ticker = T1.Ticker
WHERE T1.DateAdded IS NULL

您也可以使用 not exists:

SELECT T3.DateAdded, T3.Query_name, T3.Fund, T3.Ticker 
FROM table_name03 T3 
WHERE NOT EXISTS (
    SELECT 1
    FROM table_name01 T1 
    WHERE
        T3.DateAdded = T1.DateAdded 
        AND T3.Query_name = T1.Query_name 
        AND T3.Fund = T1.Fund
        AND T3.Ticker = T1.Ticker
)