Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 22nd, 2006, 01:05 AM
_DS
Guest
 
Posts: n/a
Default Many-to-Many with SQL

I'm not sure if there is a difference in syntax for Access's SQL
commands. I'm having trouble with what should be a very simple
compound join statement.

An example: 'Authors' table has a field called AuthorID. A 'Books'
table has a field called BookID. Since an author can write more than
one book, and a book can be written by multiple authors, this calls
for a link table ("bridge table" "association table" whatever). Call
that table 'AuthorsBooks.' It has only two fields, AuthorID and
BookID, which match the other id fields.

Robert Veiera's book on SQL (not on Access per se) spells out a query
for exactly the operation above. Similar to:
Select Authors.AuthorName, Books.BookTitle
FROM Authors
INNER JOIN AuthorsBooks ON Authors.AuthorID = AuthorsBooks.AuthorID
INNER JOIN Books ON AuthorsBooks.BookID = Books.BookID

Veiera's code is similar but again, written in SQL Server's dialect.
I'm presuming that Access is looking for slightly different syntax.
Access's error message only narrows it to the 'FROM' statement.
Ideas?

  #2  
Old February 22nd, 2006, 01:45 AM
tina
Guest
 
Posts: n/a
Default Re: Many-to-Many with SQL

i threw together the tables you described, and write the query to match the
SQL you posted. it worked for me; the only difference in the statement was
the automatic placement of parentheses (i built the query in the design
grid) by Access:

SELECT Authors.AuthorName, Books.BookTitle
FROM (Authors INNER JOIN AuthorsBooks ON Authors.AuthorID =
AuthorsBooks.AuthorID) INNER JOIN Books ON AuthorsBooks.BookID =
Books.BookID

hth


"_DS" <_DS@__nomail.com> wrote in message
news:ft8nv1pob6jfeo1dk2b685voqnvjg1sclc@4ax.com...[color=blue]
> I'm not sure if there is a difference in syntax for Access's SQL
> commands. I'm having trouble with what should be a very simple
> compound join statement.
>
> An example: 'Authors' table has a field called AuthorID. A 'Books'
> table has a field called BookID. Since an author can write more than
> one book, and a book can be written by multiple authors, this calls
> for a link table ("bridge table" "association table" whatever). Call
> that table 'AuthorsBooks.' It has only two fields, AuthorID and
> BookID, which match the other id fields.
>
> Robert Veiera's book on SQL (not on Access per se) spells out a query
> for exactly the operation above. Similar to:
> Select Authors.AuthorName, Books.BookTitle
> FROM Authors
> INNER JOIN AuthorsBooks ON Authors.AuthorID = AuthorsBooks.AuthorID
> INNER JOIN Books ON AuthorsBooks.BookID = Books.BookID
>
> Veiera's code is similar but again, written in SQL Server's dialect.
> I'm presuming that Access is looking for slightly different syntax.
> Access's error message only narrows it to the 'FROM' statement.
> Ideas?
>[/color]


  #3  
Old February 22nd, 2006, 01:55 AM
Bob Quintal
Guest
 
Posts: n/a
Default Re: Many-to-Many with SQL

_DS <_DS@__nomail.com> wrote in
news:ft8nv1pob6jfeo1dk2b685voqnvjg1sclc@4ax.com:
[color=blue]
> I'm not sure if there is a difference in syntax for Access's
> SQL commands. I'm having trouble with what should be a very
> simple compound join statement.
>
> An example: 'Authors' table has a field called AuthorID. A
> 'Books' table has a field called BookID. Since an author can
> write more than one book, and a book can be written by
> multiple authors, this calls for a link table ("bridge table"
> "association table" whatever). Call that table
> 'AuthorsBooks.' It has only two fields, AuthorID and BookID,
> which match the other id fields.
>
> Robert Veiera's book on SQL (not on Access per se) spells out
> a query for exactly the operation above. Similar to:
> Select Authors.AuthorName, Books.BookTitle
> FROM Authors
> INNER JOIN AuthorsBooks ON Authors.AuthorID =
> AuthorsBooks.AuthorID INNER JOIN Books ON
> AuthorsBooks.BookID = Books.BookID
>
> Veiera's code is similar but again, written in SQL Server's
> dialect. I'm presuming that Access is looking for slightly
> different syntax. Access's error message only narrows it to
> the 'FROM' statement. Ideas?
>[/color]
SELECT authors.authorname, books.booktitle
FROM (
authors
INNER JOIN authorsbooks ON authors.authorid =
authorbook.authorid
)
INNER JOIN books ON authorbook.bookid = books.bookid;

Access insists on the parentheses.





--
Bob Quintal

PA is y I've altered my email address.
  #4  
Old February 23rd, 2006, 01:15 AM
_DS
Guest
 
Posts: n/a
Default Re: Many-to-Many with SQL

On Wed, 22 Feb 2006 01:27:20 GMT, Bob Quintal <rquintal@sympatico.ca>
wrote:
[color=blue]
>_DS <_DS@__nomail.com> wrote in
>news:ft8nv1pob6jfeo1dk2b685voqnvjg1sclc@4ax.com :
>[color=green]
>> I'm not sure if there is a difference in syntax for Access's
>> SQL commands. I'm having trouble with what should be a very
>> simple compound join statement.[/color][/color]
[color=blue]
>SELECT authors.authorname, books.booktitle
>FROM (
> authors
> INNER JOIN authorsbooks ON authors.authorid =
> authorbook.authorid
> )
> INNER JOIN books ON authorbook.bookid = books.bookid;
>
>Access insists on the parentheses.[/color]

Tina and Bob: I hadn't seen any mention of parens in the SQL books I
own, so I presume it is an Access peculiarity. Thanks for the
followup.

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles