Mam prostą tabelę z kolumnami nazwisko i departament.
Chciałbym wybrać te departamenty gdzie nie występuje pracownik o okreslonym nazwisku.
Mogę prosić o podpowiedź ?
Dzięki.
Mniej więcej coś takiego:
SELECT departament FROM tabela WHERE nazwisko != 'to_ktorego_nie_chcesz'
tak próbowałem ale to nie działa bo :
jezeli mamy:
departament - 4, nazwisko - kowalski
departament - 4, nazwisko - nowak
to przy takim zapytaniu zwróci i tak departament 4
To w takim razie może tak:
SELECT departament FROM tabela WHERE departament != (SELECT departament FROM tabela WHERE nazwisko = 'to_ktorego_nie_chcesz')
uff, poradzilem sobie :)
SELECT departament FROM tabela WHERE tabela.departament not in (SELECT departament from tabela where nazwisko = 'to_ktorego_nie_chcesz')
wystarczy
SELECT departament FROM tabela WHERE nazwisko <> 'to którego nie chcesz'
SELECT distinct departament FROM tabela WHERE tabela.departament not in (SELECT departament from tabela where nazwisko = 'to_ktorego_nie_chcesz')
albo tak
SELECT distinct departament FROM tabela a WHERE (SELECT count(*) from tabela b where b.nazwisko = 'to_ktorego_nie_chcesz' and b.departament = a.departament )=0
http://sqlfiddle.com/#!11/1ae8f/2
Moje (ostatnie), nie dość, że zwraca dobre wyniki, to jeszcze czasem wygrywa :)
Można i tak:
select departament
from tabela
group by departament
having list(distinct(nazwisko)) not containing 'kowalski'