mysql - Find not common users in sql -


i have 5 tables. want specific users in table 1 not in table2, table3, table4 , table5. can please me :)

table1(userid,discount) table2(userid,discount) table3(userid,discount) table4(userid,discount) table5(userid,discount) 

the following query:

select userid table1 t1 not exists (   select 1    (     select userid     table2     union      select userid     table3     union      select userid     table4     union      select userid     table5) t2   t1.userid = t2.userid) 

returns users of table1 don't exist in of other tables.

demo here

if want, say, users of table2 don't exist in of other tables, can modify above query return users table2 , perform union between these 2 queries:

select userid table1 t1 not exists (   select 1    (     select userid     table2     union      select userid     table3     union      select userid     table4     union      select userid     table5) t2   t1.userid = t2.userid)  union  select userid table2 t1 not exists (   select 1    (     select userid     table1     union      select userid     table3     union      select userid     table4     union      select userid     table5) t2   t1.userid = t2.userid) 

demo here

the above can extended incorporate users tables. but, have admit, becomes quite verbose !


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -