Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/recipe-books.git

Incorrect join for plucking images.

Author Dave Jarvis <email>
Date 2013-04-08 08:40:45 GMT-0700
Commit 1ca930fd94c418c017a0febda9fdb36fba40e0b2
Parent fff2ee8
sql/views/book_photograph_vw.sql
--- DROP VIEW recipe_book.book_photograph_vw;
-
-CREATE OR REPLACE VIEW recipe_book.book_photograph_vw AS
+CREATE OR REPLACE VIEW recipe_book.book_photograph_vw AS
+-- Get photographs associated with a recipe within a book.
SELECT
rp.id AS photograph_id,
WHERE
rp.id = rrp.photograph_id AND
- rp.id = rbbr.recipe_id AND
+ rrp.id = rbbr.recipe_id AND
rp.photograph_category_id = rpc.id AND
rpc.classification = 'RECIPE'
UNION ALL
+-- Get photographs associated with a book, but not a recipe.
SELECT
rp.id AS photograph_id,
rp.id = rbbp.photograph_id AND
rp.photograph_category_id = rpc.id AND
- rpc.classification = 'BOOK';
+ rpc.classification = 'BOOK'
+UNION ALL
+-- Get the recipe photographs that aren't part of a book (yet).
+SELECT
+ rp.id AS photograph_id,
+ NULL AS book_id,
+ rrp.recipe_id,
+ rp.image_url,
+ rp.photograph_category_id,
+ rpc.label AS photograph_category
+FROM
+ recipe.photograph rp,
+ recipe.photograph_category rpc,
+ recipe.recipe_photograph rrp
+WHERE
+ rrp.photograph_id = rp.id AND
+ rp.photograph_category_id = rpc.id AND
+ NOT EXISTS(
+ SELECT
+ recipe_id
+ FROM
+ recipe_book.book_recipe rbbr
+ WHERE rbbr.recipe_id = rrp.recipe_id
+ );
+
Delta 29 lines added, 5 lines removed, 24-line increase