MySQL Inner Join Query To Get Records Not Present in Other Table

后端 未结 4 1040
北海茫月
北海茫月 2021-02-05 15:23

I have table 1, all_countries, as follows-

id   |  country
------------------
1    |  USA
2    |  China
3    |  India
4    |  France
5    |  UK
6    |  Australia         


        
相关标签:
4条回答
  • 2021-02-05 15:52

    SELECT ac.country FROM all_countries ac LEFT JOIN supported_countries sc ON sc.country_name = ac.country_name WHERE ISNULL(sc.country_name)

    0 讨论(0)
  • 2021-02-05 15:52

    While @Joachim Isaksson gave me the clue, I tested this very similar query and worked on my database by replacing variables.

    SELECT * FROM all_countries 
    LEFT JOIN supported_countries ON all_countries.id = supported_countries.id 
    WHERE supported_countries.id IS NULL
    

    I gave the question and that Joachim's answer my thumbs up. Cheers !

    0 讨论(0)
  • 2021-02-05 16:09

    A LEFT JOIN will do that elegantly;

    SELECT a.* 
    FROM all_countries a
    LEFT JOIN supported_countries s
      ON a.country = s.country
    WHERE s.id IS NULL;
    

    Demo here.

    0 讨论(0)
  • 2021-02-05 16:09

    Try something like the following:

    SELECT * FROM all_countries 
      WHERE country NOT IN (SELECT country FROM supported_countries)
    
    0 讨论(0)
提交回复
热议问题