473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Linking table SQL help needed

I can't come up with a query that works. Can anyone help?
Conceptually the relationships are easy to describe. I have
a table for books (Entries), a table for authors (Authors),
and a linking table between the two because books often
have more than one author (OA_Link). This situation is
simple and common. A query to list each title and all
the authors associated with that title looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_EntryID)
ON Authors.AuthorID = OA_Link.OA_NameID
WHERE Authors.AuthorName Like "*Twain*";

Unfortunately there is an added twist which I can't
resolve. There is also present the concept of the
"Main Author." The single individual who holds primary
responsibility for the book. The designer of the
database chose to create a one-to-one link directly
from Entries to Authors without going through the
linking table. With the query above, I can only retrieve
books which have "Twain" as an added author, but not
when Twain is the principle author. If I do a query
on principle authors only, it looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors INNER JOIN Entries
ON Authors.AuthorID = Entries.AuthorID
WHERE Authors.AuthorName Like "*Twain*";

How do I combine the two to do a query that searches
both Authors as Principle Author as well as Authors as
Added Authors? I need a pure SQL solution rather than
something that uses saved queries.

Thanks to anyone who can help me!

Arvin

Nov 12 '05 #1
5 1685
Ahh, I see what I need here is a UNION. In fact taking
the two queries listed below and sticking "UNION" between
them does the trick. Sorry for my ignorance but I've
never used a union before.

I guess I have a second question. My application allows
the user to search on multiple fields, Author, Title,
Subject, Call Number, all in the context of some complicated
joins. How do I incorporate a UNION into something like
that. I can see how to do it now as a standalone author
search, but not how to incorporate it into a larger SQL
statement.
Arvin Portlock wrote:
I can't come up with a query that works. Can anyone help?
Conceptually the relationships are easy to describe. I have
a table for books (Entries), a table for authors (Authors),
and a linking table between the two because books often
have more than one author (OA_Link). This situation is
simple and common. A query to list each title and all
the authors associated with that title looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_EntryID)
ON Authors.AuthorID = OA_Link.OA_NameID
WHERE Authors.AuthorName Like "*Twain*";

Unfortunately there is an added twist which I can't
resolve. There is also present the concept of the
"Main Author." The single individual who holds primary
responsibility for the book. The designer of the
database chose to create a one-to-one link directly
from Entries to Authors without going through the
linking table. With the query above, I can only retrieve
books which have "Twain" as an added author, but not
when Twain is the principle author. If I do a query
on principle authors only, it looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors INNER JOIN Entries
ON Authors.AuthorID = Entries.AuthorID
WHERE Authors.AuthorName Like "*Twain*";

How do I combine the two to do a query that searches
both Authors as Principle Author as well as Authors as
Added Authors? I need a pure SQL solution rather than
something that uses saved queries.

Thanks to anyone who can help me!

Arvin


Nov 12 '05 #2
Have you tried a UNION query? This basically tacks the results of one
statement onto the other.

(SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_EntryID)
ON Authors.AuthorID = OA_Link.OA_NameID
WHERE Authors.AuthorName Like "*Twain*")
UNION
(SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors INNER JOIN Entries
ON Authors.AuthorID = Entries.AuthorID
WHERE Authors.AuthorName Like "*Twain*");

Pat.
Arvin Portlock <ap********@hotmail.com> wrote in message news:<bv***********@agate.berkeley.edu>...
I can't come up with a query that works. Can anyone help?
Conceptually the relationships are easy to describe. I have
a table for books (Entries), a table for authors (Authors),
and a linking table between the two because books often
have more than one author (OA_Link). This situation is
simple and common. A query to list each title and all
the authors associated with that title looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_EntryID)
ON Authors.AuthorID = OA_Link.OA_NameID
WHERE Authors.AuthorName Like "*Twain*";

Unfortunately there is an added twist which I can't
resolve. There is also present the concept of the
"Main Author." The single individual who holds primary
responsibility for the book. The designer of the
database chose to create a one-to-one link directly
from Entries to Authors without going through the
linking table. With the query above, I can only retrieve
books which have "Twain" as an added author, but not
when Twain is the principle author. If I do a query
on principle authors only, it looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors INNER JOIN Entries
ON Authors.AuthorID = Entries.AuthorID
WHERE Authors.AuthorName Like "*Twain*";

How do I combine the two to do a query that searches
both Authors as Principle Author as well as Authors as
Added Authors? I need a pure SQL solution rather than
something that uses saved queries.

Thanks to anyone who can help me!

Arvin

Nov 12 '05 #3
Try this - I built the three tables you described and it seems to do the
job.

SELECT Entries.EntryID, Entries.TitleStatement, Authors.AuthorName AS
Pr_Auth, Authors_1.AuthorName AS All_Auths
FROM ((Authors INNER JOIN Entries ON Authors.AuthorID =
Entries.AuthorID) INNER JOIN OA_Link ON Entries.EntryID =
OA_Link.OA_EntryID) INNER JOIN Authors AS Authors_1 ON OA_Link.OA_NameID
= Authors_1.AuthorID
ORDER BY Entries.EntryID, Authors.AuthorName;

Bruce Pick
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++
To send email, remove all < > and [ ] and the junk ~ between:
[brucepick1]
< a~t >
[comcast.net]

Arvin Portlock wrote:
I can't come up with a query that works. Can anyone help?
Conceptually the relationships are easy to describe. I have
a table for books (Entries), a table for authors (Authors),
and a linking table between the two because books often
have more than one author (OA_Link). This situation is
simple and common. A query to list each title and all
the authors associated with that title looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_EntryID)
ON Authors.AuthorID = OA_Link.OA_NameID
WHERE Authors.AuthorName Like "*Twain*";

Unfortunately there is an added twist which I can't
resolve. There is also present the concept of the
"Main Author." The single individual who holds primary
responsibility for the book. The designer of the
database chose to create a one-to-one link directly
from Entries to Authors without going through the
linking table. With the query above, I can only retrieve
books which have "Twain" as an added author, but not
when Twain is the principle author. If I do a query
on principle authors only, it looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors INNER JOIN Entries
ON Authors.AuthorID = Entries.AuthorID
WHERE Authors.AuthorName Like "*Twain*";

How do I combine the two to do a query that searches
both Authors as Principle Author as well as Authors as
Added Authors? I need a pure SQL solution rather than
something that uses saved queries.

Thanks to anyone who can help me!

Arvin

Nov 12 '05 #4
Here's a better solution - lets yo specify an author, and get all books
they worked on, with the correct primary author.

SELECT Entries.EntryID, Entries.TitleStatement, Authors_1.AuthorName AS
MainAuth, Authors.AuthorName AS AllAuth
FROM (Entries INNER JOIN (Authors INNER JOIN OA_Link ON Authors.AuthorID
= OA_Link.OA_NameID) ON Entries.EntryID = OA_Link.OA_EntryID) INNER JOIN
Authors AS Authors_1 ON Entries.AuthorID = Authors_1.AuthorID
WHERE (((Authors.AuthorName)="twain"))
ORDER BY Entries.EntryID, Authors.AuthorName;

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++
To send email, remove all < > and [ ] and the junk ~ between:
[brucepick1]
< a~t >
[comcast.net]
Arvin Portlock wrote:
I can't come up with a query that works. Can anyone help?
Conceptually the relationships are easy to describe. I have
a table for books (Entries), a table for authors (Authors),
and a linking table between the two because books often
have more than one author (OA_Link). This situation is
simple and common. A query to list each title and all
the authors associated with that title looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_EntryID)
ON Authors.AuthorID = OA_Link.OA_NameID
WHERE Authors.AuthorName Like "*Twain*";

Unfortunately there is an added twist which I can't
resolve. There is also present the concept of the
"Main Author." The single individual who holds primary
responsibility for the book. The designer of the
database chose to create a one-to-one link directly
from Entries to Authors without going through the
linking table. With the query above, I can only retrieve
books which have "Twain" as an added author, but not
when Twain is the principle author. If I do a query
on principle authors only, it looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors INNER JOIN Entries
ON Authors.AuthorID = Entries.AuthorID
WHERE Authors.AuthorName Like "*Twain*";

How do I combine the two to do a query that searches
both Authors as Principle Author as well as Authors as
Added Authors? I need a pure SQL solution rather than
something that uses saved queries.

Thanks to anyone who can help me!

Arvin

Nov 12 '05 #5
I tried this query and it only pulled up entries where
Twain was one of the alternate authors. It didn't retrieve
any where he was the primary author. I added another
parameter to the WHERE clause to get this:

SELECT Entries.TitleStatement, Authors_1.AuthorName AS MainAuth,
Authors.AuthorName AS AllAuth
FROM (Entries INNER JOIN (Authors INNER JOIN OA_Link
ON Authors.AuthorID = OA_Link.OA_NameID)
ON Entries.EntryID = OA_Link.OA_EntryID)
INNER JOIN Authors AS Authors_1
ON Entries.AuthorID = Authors_1.AuthorID

WHERE ((Authors.AuthorName LIKE "*twain*")
OR (Authors_1.AuthorName LIKE "*twain*"))

ORDER BY Entries.EntryID, Authors.AuthorName;

And it retrieved more records. But what it *didn't* retrieve
were records where twain was the primary author but there were
no alternate authors at all for the record. Hmmm, this is so
close. I couldn't get UNION to do what I wanted, if I could
figure out how to tweak this solution to do everything it
would be ideal.

BTW. I knew you could alias field names, like Authors.AuthorName
AS AllAuth, but I didn't know you could alias tables in your join
sections, like INNER JOIN Authors AS Authors_1. I only started
working with databases about a year ago and I only learn new SQL
as the job demands. This trick looks useful, if I could only
figure out exactly what it does.

Thanks for spending the time to really help out with this!

Arvin
Bruce Pick wrote:
Here's a better solution - lets yo specify an author, and get all books
they worked on, with the correct primary author.

SELECT Entries.EntryID, Entries.TitleStatement, Authors_1.AuthorName AS
MainAuth, Authors.AuthorName AS AllAuth
FROM (Entries INNER JOIN (Authors INNER JOIN OA_Link ON Authors.AuthorID
= OA_Link.OA_NameID) ON Entries.EntryID = OA_Link.OA_EntryID) INNER JOIN
Authors AS Authors_1 ON Entries.AuthorID = Authors_1.AuthorID
WHERE (((Authors.AuthorName)="twain"))
ORDER BY Entries.EntryID, Authors.AuthorName;

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++
To send email, remove all < > and [ ] and the junk ~ between:
[brucepick1]
< a~t >
[comcast.net]
Arvin Portlock wrote:
I can't come up with a query that works. Can anyone help?
Conceptually the relationships are easy to describe. I have
a table for books (Entries), a table for authors (Authors),
and a linking table between the two because books often
have more than one author (OA_Link). This situation is
simple and common. A query to list each title and all
the authors associated with that title looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_EntryID)
ON Authors.AuthorID = OA_Link.OA_NameID
WHERE Authors.AuthorName Like "*Twain*";

Unfortunately there is an added twist which I can't
resolve. There is also present the concept of the
"Main Author." The single individual who holds primary
responsibility for the book. The designer of the
database chose to create a one-to-one link directly
from Entries to Authors without going through the
linking table. With the query above, I can only retrieve
books which have "Twain" as an added author, but not
when Twain is the principle author. If I do a query
on principle authors only, it looks like this:

SELECT Entries.TitleStatement, Authors.AuthorName
FROM Authors INNER JOIN Entries
ON Authors.AuthorID = Entries.AuthorID
WHERE Authors.AuthorName Like "*Twain*";

How do I combine the two to do a query that searches
both Authors as Principle Author as well as Authors as
Added Authors? I need a pure SQL solution rather than
something that uses saved queries.

Thanks to anyone who can help me!

Arvin

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external...
2
by: Ryan | last post by:
Little puzzle this. I need to link from SQL2000 to Access to get to a table held within an Access DB. Now you can link within Access to a SQL table, but can this be done the other way round ?...
5
by: Brian | last post by:
I need to import data from 720 csv files into an Access database so I can do some editing prior to loading into a SQL Server. These files came from data output from a mainframe on a monthly basis....
11
by: dskillingstad | last post by:
I've been struggling with this problem for some time and have tried multiple solutions with no luck. Let me start with, I'm a novice at Access and I'm not looking for someones help to design my...
1
by: hmiller | last post by:
I'm sorry to populate the server with yet another question about linking multiple tables and queries, howerver I have not been able to find the right criteria. My problem. I am trying to...
4
by: Tyler | last post by:
I have a database where I would like to enter some data. This data would fill a table in that database but it would also fill in a table in another. Is this possible to do? Do I need to have...
2
by: pssraju | last post by:
Hi, At present application was built on solaris 9 using sun studio 9 (Sun C++ 5.6) & rouguewave sorce pro 5. We are planning to port the same application onto SuSE Linux 9.5.0 using GCC 3.3.3 & RW...
7
by: Salad | last post by:
I am converting an application from A97 to A2003. I have 2 tables created by another application as a Foxpro.dbf. The table has no index. The connect string in A97 is FoxPro...
2
by: grahto | last post by:
Thanks in advance for anyone who can provide me with some help. I am completely stumped. I haven't used Access to design anything more than a simple database with separate tables in over a year. I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.