简体   繁体   中英

SQL select records

i have two tables

--table a--
first  second   
------ -------  
user-1 user-2
user-2 user-1
user-2 user-3

--table b--
ignoreby  ignored
--------  -------
user-1    user-2
user-1    user-4
user-3    user-4

i want to select the records from table a which are not ignoring themself(including unidirectional records) on table b

The result here would be: user-2 user-3

The record user-2 user-1 should not be included in the result because user-1 ignores user-2 and this is unidirectional.

Thanks

Do you mean "bidirectional" and not "unidirectional"? If so:

select a.*
from tablea a
where not exists (select 1
                  from tableb b
                  where (b.ignoreby = a.first and b.ignored = a.second) or
                        (b.ignoreby = a.second and b.ignored = a.first)
                 );

If you really do mean "unidirectional", then you would only use the first condition.

Maybe you can try a different and more simpler approach. Like adding a column called is_ignored with a boolean value to your first table (Table a). Then you can directly see with a single select if the user ignored the other user.

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