Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Many-to-many relationships in databases

by leriksen (Curate)
on Oct 28, 2005 at 06:32 UTC ( [id://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

Replies are listed 'Best First'.
Re^2: Many-to-many relationships in databases
by Corion (Patriarch) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://503571]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found