Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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
Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2015-11-25 20:25 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (690 votes), past polls