简体   繁体   中英

SQL Select where condition : value 1 <> value 2

Need your help to know if possible to select values from a table with the below condition :

Table content : matching between 2 objects (Id_obj_A; name_obj_A; country_obj_A; Id_obj_B; name_obj_B; country_obj_B)

Select *
from table
Where (only if country_obj_A <> country_obj_B)

Many thanks for your help

Yes. There are a few ways, one is to use NOT EXISTS like this:

select
       *
from tableA
where NOT EXISTS (
   select NULL
   from tableB
   where tableB.country_obj_B = tableA.country_obj_A
   )

or, using NOT IN

select
       *
from tableA
where country_obj_A NOT IN (
   select country_obj_B 
   from tableB
   )

or, using a LEFT JOIN then exclude the joined rows:

select
       *
from tableA
left join tableB on tableA.country_obj_A = tableB.country_obj_B
where tableB.country_obj_B IS NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM