Last active
August 29, 2015 14:02
-
-
Save hrach/a814eb6e715080fa781a to your computer and use it in GitHub Desktop.
@hrachcz: #anketa. model: author 1:n books m:n tags. zajima me sql, ktere vybere autory, kteri nenapsali zadnou knihu o PHP (tj. bez tagu PHP).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select * | |
from authors | |
where not exists ( | |
select * | |
from books | |
left join books_x_tags on (books_x_tags.book_id = books.id) | |
left join tags on (tags.id = books_x_tags.tag_id) | |
where | |
books.author_id = authors.id | |
and tags.name = 'PHP' | |
) |
@ZZromanZZ To je špatně. Vypadnou Ti autoři bez knih (sporné zadání) a dostaneš i autora, který napsal více knih včetně jedné o PHP.
@milo 👍
Pravda, netestuju vysledek.
Co tohle ?
SELECT * FROM author WHERE authorID IN (SELECT authorID FROM book WHERE bookID NOT IN (SELECT bookID FROM tag_x_book WHERE tagID = 'PHP'))
Ale uz si nemyslim(pokud je to dobre), ze to bude nejak vyrazne rychle...
@milo no jasny :) 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SELECT * FROM author INNER JOIN book USING (authorID)
WHERE bookID NOT IN (SELECT bookID FROM tag_x_book WHERE tagID = 'PHP')
-- nutnost indexu nad vsemi idecky
-- vnitrni select nesmi byt wildcard
-- USING netreba pouzivat, nechtela se mi rozepisovat podminka
-- jedna se o typicky priklad relacniho rozdilu
-- takto postaveny dotaz by mohl byt solidne optimalizovany a rychly i v MySQL