If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Searching a many-to-many architecture

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-26-06, 15:35
mendingo mendingo is offline
Registered User
 
Join Date: May 2003
Posts: 5
Searching a many-to-many architecture

Say I have a database for storing info on books, and I want to search by book title or author.

Books can be by multiple authors, so this is a many-many relationship.

I have normalised my tables and ended up with this:

Books(book_ID(pk), book_name)
book_authors(book_ID(pk), author_id(pk))
authors(author_id(pk), author_name)

I have in the database two authors, say "Mr Smith" and "Mr Jones", and 3 books, "Book A", "Book B" and "Book C"

"Book A" is written by Mr Smith
"Book B" is written by Mr Smith and Mr Jones
"Book C" is written by Mr Jones

How can I now search for a book that is written by BOTH Mr Jones AND Mr Smith?

It should return just Book B.

It can be done with a nested SELECT Query in SQL, but then what if there are three authors to a book, I'd have to add another layer of nesting.

Is there a more efficient way to do this?
Reply With Quote
  #2 (permalink)  
Old 01-27-06, 01:07
ByteRyder52 ByteRyder52 is offline
Registered User
 
Join Date: Feb 2005
Location: Colorado Springs
Posts: 222
It looks to me like this is an intersect.
Reply With Quote
  #3 (permalink)  
Old 01-27-06, 08:20
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Unfortunately there aren't just one kind of many-to-many relationships, so there isn't a "one size fits all" solution for this kind of problem. If you're using the classic many-to-many relationship, then the solution is easier...

Write a SELECT statement that will retrieve all of the appropriate rows for this many-to-many relationship. Use the WHERE clause to filter out only the author(s) of interest. Count the returned rows, and compare that count (two in this case) against the optimal count (two, one each for Jones and Smith). If the counts are equal, you've got a "full solution".

One thing that you need to be careful to decide what constitutes a "correct" answer for in you case is over counts. For instance, if you expect two, but get four (because Mrs Smith and Mrs Jones were co-authors too), how does that affect your answer?

-PatP
Reply With Quote
  #4 (permalink)  
Old 01-28-06, 17:05
mendingo mendingo is offline
Registered User
 
Join Date: May 2003
Posts: 5
Quote:
Originally Posted by Pat Phelan
Write a SELECT statement that will retrieve all of the appropriate rows for this many-to-many relationship. Use the WHERE clause to filter out only the author(s) of interest. Count the returned rows, and compare that count (two in this case) against the optimal count (two, one each for Jones and Smith). If the counts are equal, you've got a "full solution".
Could you elaborate on this a little, please?
Reply With Quote
  #5 (permalink)  
Old 01-29-06, 14:07
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Sure. Check out Brett Kaiser's great post on how to get answers quickly via forums. Post the basic information needed, and I'll cook you up an example instead of just explaining more about the abstract idea of this solution.

-PatP
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On