Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Many-to-many relationships in databases

by leriksen (Curate)
on Oct 28, 2005 at 06:32 UTC ( #503571=note: print w/ replies, xml ) Need Help??


in reply to Many-to-many relationships in databases : SOLVED

I'm not the strongest on SQL, but I think you can use 'in' to solve your problem i.e.

SELECT book FROM BooksAuthors WHERE Author in ('Jones', 'Smith');

SQL syntax may not be perfect - not my main field of expertise

Also look at SQL::Abstract and the extension to Class::DBI that uses it, Class::DBI::AbstractSearch, for an easy way to build these queries dynamically using perl data structures, for any length list of authors.

...reality must take precedence over public relations, for nature cannot be fooled. - R P Feynmann


Comment on Re: Many-to-many relationships in databases
Download Code
Re^2: Many-to-many relationships in databases
by Corion (Pope) on Oct 28, 2005 at 06:37 UTC

    That's close but will also return books authored by only one of 'Jones' or 'Smith' alone. Of course, some quick Perl filtering might be easier than doing it in SQL.

      I don't think that's true, unless different SQL engines implement IN differently.
      On all the engines I've used, IN is equivalent to a list of OR statements.

        How does that contradict my statement? The BookAuthors table seems to contain one entry for each pair (book, author), so (in a contrived example) it could look like the following:

        book author -------------------- 1 Jones 1 Miller 2 Jones 2 Smith 3 Smith 4 Jones

        The statement will return every book id for the given query of author IN ('Jones', 'Smith'), because, as you say, IN is more or less equivalent to a series of OR statements (I'm not sure about differences regarding NULL values).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://503571]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2014-10-21 18:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (106 votes), past polls