MySQL find the difference between two tables (non-intersection)

How to query records with inconsistent data in two tables with different fields

Generally, you can use NOT EXISTS (non-existent clause) or LEFT JOIN left (right) connection to generate the empty field value to filter the difference between the two tables

1、NOT EXISTS 

Not exists when the matching field has an index that is available, its operating efficiency is very high, but if it is run on a large data table without an index, its operating efficiency is extremely poor, and it should be avoided

SELECT
    * 
FROM
    smd_employee t1 
WHERE
    NOT EXISTS ( SELECT 1 FROM asd_user_account t2 WHERE t2.u_phone = t1.employee_phone );

 2、LEFT JOIN

Use the left (right) connection to find the difference set, because the need to implement the two table connection will cause the Cartesian effect of the output set of record rows may increase,

If it is not one-to-one or one-to-many, we should treat the many-to-many situation as many-to-one before connecting, otherwise the output record set may be incorrect.

SELECT
    a.* 
FROM
    smd_employee a
    LEFT JOIN asd_user_account b ON b.u_phone = a.employee_phone 
WHERE
    b.u_phone IS NULL;

 Two methods of finding the difference set, when an index is available, the efficiency of not exists is higher than that of left join, otherwise the efficiency of left join is better

Original reference: https://zhidao.baidu.com/question/565882811554863764.html

Guess you like

Origin www.cnblogs.com/bigroc/p/12750005.html