Top

Tags: DB

MySQL Statements

Oct 21, 2020 | 615 views

#DB


差集

SELECT t1.id, t1.name, t1.age
    FROM t1 
    LEFT JOIN t2 
    ON t1.id = t2.id
    WHERE t1.name != t2.name
    
       OR t1.age != t2.age;
    
    
    id	name	age
    2	小宋	    20
    3	小白	    30

交集

SELECT  id,  NAME,  age, COUNT(*)
    FROM (SELECT id, NAME, age
        FROM t1
        
        UNION ALL
        
        SELECT id, NAME, age
        FROM t2
        ) a
    GROUP BY id, NAME, age
    HAVING COUNT(*) > 1
    
    id	NAME	age	COUNT(*)
    1	小王	    10	2
    4	hello	40	2


Comments: 0