Jak zwrócić dołączone tabele, nawet jeśli nie mają odpowiedników?

0

Witam, dzien 4 nauki sql

moje zadanie:

Show all books with their movie adaptations. Select each book's title, the name of its publishing house, the title of its adaptation, and the type of the adaptation. Keep the rows with no adaptation in the result.

moje rozwiazanie;

SELECT
book.title,
book.publishing_house,
adaptation.title
adaptation.type
FROM book
LEFT JOIN adaptation
on book.id = adaptation.book_id
WHERE adaptation.type = 'movie';

Moje pytanie:

Moze mi ktos logicznie wytlumaczyc dlaczego brakuje na koncu OR type IS NULL; - wiadomo, ze odnosi sie to do "Keep the rows with no adaptation in the result".

Lecz czy wlasnie nie po to uzylem LEFT JOINa? zeby pozycje z null tez wyswietlic?
Pozdrawiam wszystkich !

0

WHERE filtruje całość zapytania, jak filtrujesz po kolumnach z left/right joina to ten filtr musi być w ON joina a nie w where

0

Twoje zapytanie powinno wyglądać tak (chyba, nie mam jak teraz sprawdzić)

SELECT
book.title,
book.publishing_house,
adaptation.title
adaptation.type
FROM book
LEFT JOIN adaptation
on (book.id = adaptation.book_id AND adaptation.type = 'movie');

czyli jesli JOIN da wyniki z nullem a uzyje potem where to znowu musze dac odnosnik do null? bo tamten niweluje?

Tak

0

Pro tip

Mamy XXI wiek i chat gpt.

Wpisz w niego

SQL for
Show all books with their movie adaptations. Select each book's title, the name of its publishing house, the title of its adaptation, and the type of the adaptation. Keep the rows with no adaptation in the result.

I będziesz mial z opisem rozwiązanie.

0
marian pazdzioch napisał(a):

Mamy XXI wiek i chat gpt.

Wpisz w niego

SQL for
Show all books with their movie adaptations. Select each book's title, the name of its publishing house, the title of its adaptation, and the type of the adaptation. Keep the rows with no adaptation in the result.

I będziesz mial z opisem rozwiązanie.

Problem z ChatGPT jest taki, że czasem wypluwa niepoprawne rozwiązania.

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.