skip to Main Content

Table 1:

Name    Address   Height    Weight
X.        y         z.        p
G.        H.        I         J
Q         W.        E.        R

Table 2:

Name    Income     Tax    
X.        y         z.      

Table 1 has more rows than table 2. All the Names in table 2 are in table 1 but not all the names of table 1 are in table 2.
Is there anyway to compare the rows between the two tables and get the names that appear only in table 1 and not in table 2 as well? mySQL

3

Answers


  1. Try this…

    WITH   - S a m p l e    D a t a :
      tbl_1 ( Name, Address, Height, Weight ) AS
        ( Select 'X.',      'y',         'z.',       'p'   Union All
          Select 'G.',      'H.',        'I',        'J'   Union All
          Select 'Q',       'W.',        'E.',       'R'
        ), 
      tbl_2 ( Name,  Income,  Tax  ) AS  
        ( Select 'X.',   'y',    'z.'  )
    
    --     S Q L :
    Select     t1.*
    From       tbl_1 t1
    Left Join  tbl_2 t2 ON(t2.Name = t1.Name)
    Where      t2.Name Is Null;
    
    Login or Signup to reply.
  2. You can easily set others to null I believe if you are using mySQL.

    SELECT t1.Name
    FROM Table1 t1
    LEFT JOIN Table2 t2 ON t1.Name = t2.Name
    WHERE t2.Name IS NULL;
    
    Login or Signup to reply.
  3. Use common

    SELECT name FROM table_1
    EXCEPT
    SELECT name FROM table_2
    

    Set Operations with UNION, INTERSECT, and EXCEPT

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search