Mysql for many relationship queries

Maybe I'm just falling asleep (or not!), But how can you do this:

I have a table (many for many), say, for example, with the key1 and key2 fields in which I want to select all key1 that are not related to a specific key2. As an example, if I have the following:

k1_A --- k2_A 
k1_A --- k2_B
k1_B --- k2_C
k1_C --- k2_D
k1_D --- k2_A

I want all key1 to not have "k2_A", so I would expect the result: k1_B, k1_C.

Thanks Greetings

+3
source share
1 answer
SELECT key1 
FROM table 
WHERE key1 NOT IN
(
  SELECT key1
  FROM table
  WHERE key2 = 'k2_A'
);
+3
source

Source: https://habr.com/ru/post/1735505/


All Articles