本文介绍了MySQL从table01中选择table02上不存在的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two tables, table03 have 10 rows and table01 have 21 rows, now I want to get rows from table03 where they don't exist in table01, so far I wrote this query but it shows all rows of table03 even some rows doesn't exist on 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`

Table03:

Table01:

推荐答案

您处在正确的轨道上.您只需添加一个 where 条件即可对不匹配的行进行过滤:

You are on the right track. You can just add a where condition to filter on unmatched rows:

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

您还可以使用不存在:

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
)

这篇关于MySQL从table01中选择table02上不存在的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 10:42