Had a interesting problem where we were looking for items in one table that were not in another table.
Say you have Books and those books can only be sold in particular countries. Say one book you could sell in the USA or CAN but it can't be sold in FRA or AUS. So two tables Orders and Book_Countries where Book Countries consist of book_name and book_country as the two columns which is the composite primary key.
Now you have orders put in for books and you need to determine if there are any orders where a book has been requested where it isn't authorized for sale in the country the order was placed from.
You can find this either of the two below:
SELECT DISTINCT o.ord_id, o.book, o.country FROM Orders o
LEFT OUTER JOIN Book_Countries b
ON o.book = b.book
AND o.country = b.country
WHERE b.book is null
SELECT DISTINCT o.order_id, o.book, o.country FROM Orders o
WHERE NOT o.country IN (SELECT b.country FROM Book_Countries b where b.book = o.book)
This will get you a list of orders that have been placed that have books on them that are not authorized for sale in the country on the order.
No comments:
Post a Comment