473,656 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.TitleSt atement, Authors.AuthorN ame
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_Entr yID)
ON Authors.AuthorI D = OA_Link.OA_Name ID
WHERE Authors.AuthorN ame 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.TitleSt atement, Authors.AuthorN ame
FROM Authors INNER JOIN Entries
ON Authors.AuthorI D = Entries.AuthorI D
WHERE Authors.AuthorN ame 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 1695
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.TitleSt atement, Authors.AuthorN ame
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_Entr yID)
ON Authors.AuthorI D = OA_Link.OA_Name ID
WHERE Authors.AuthorN ame 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.TitleSt atement, Authors.AuthorN ame
FROM Authors INNER JOIN Entries
ON Authors.AuthorI D = Entries.AuthorI D
WHERE Authors.AuthorN ame 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.TitleSt atement, Authors.AuthorN ame
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_Entr yID)
ON Authors.AuthorI D = OA_Link.OA_Name ID
WHERE Authors.AuthorN ame Like "*Twain*")
UNION
(SELECT Entries.TitleSt atement, Authors.AuthorN ame
FROM Authors INNER JOIN Entries
ON Authors.AuthorI D = Entries.AuthorI D
WHERE Authors.AuthorN ame Like "*Twain*");

Pat.
Arvin Portlock <ap********@hot mail.com> wrote in message news:<bv******* ****@agate.berk eley.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.TitleSt atement, Authors.AuthorN ame
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_Entr yID)
ON Authors.AuthorI D = OA_Link.OA_Name ID
WHERE Authors.AuthorN ame 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.TitleSt atement, Authors.AuthorN ame
FROM Authors INNER JOIN Entries
ON Authors.AuthorI D = Entries.AuthorI D
WHERE Authors.AuthorN ame 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.TitleSt atement, Authors.AuthorN ame AS
Pr_Auth, Authors_1.Autho rName AS All_Auths
FROM ((Authors INNER JOIN Entries ON Authors.AuthorI D =
Entries.AuthorI D) INNER JOIN OA_Link ON Entries.EntryID =
OA_Link.OA_Entr yID) INNER JOIN Authors AS Authors_1 ON OA_Link.OA_Name ID
= Authors_1.Autho rID
ORDER BY Entries.EntryID , Authors.AuthorN ame;

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.TitleSt atement, Authors.AuthorN ame
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_Entr yID)
ON Authors.AuthorI D = OA_Link.OA_Name ID
WHERE Authors.AuthorN ame 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.TitleSt atement, Authors.AuthorN ame
FROM Authors INNER JOIN Entries
ON Authors.AuthorI D = Entries.AuthorI D
WHERE Authors.AuthorN ame 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.TitleSt atement, Authors_1.Autho rName AS
MainAuth, Authors.AuthorN ame AS AllAuth
FROM (Entries INNER JOIN (Authors INNER JOIN OA_Link ON Authors.AuthorI D
= OA_Link.OA_Name ID) ON Entries.EntryID = OA_Link.OA_Entr yID) INNER JOIN
Authors AS Authors_1 ON Entries.AuthorI D = Authors_1.Autho rID
WHERE (((Authors.Auth orName)="twain" ))
ORDER BY Entries.EntryID , Authors.AuthorN ame;

+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++
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.TitleSt atement, Authors.AuthorN ame
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_Entr yID)
ON Authors.AuthorI D = OA_Link.OA_Name ID
WHERE Authors.AuthorN ame 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.TitleSt atement, Authors.AuthorN ame
FROM Authors INNER JOIN Entries
ON Authors.AuthorI D = Entries.AuthorI D
WHERE Authors.AuthorN ame 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.TitleSt atement, Authors_1.Autho rName AS MainAuth,
Authors.AuthorN ame AS AllAuth
FROM (Entries INNER JOIN (Authors INNER JOIN OA_Link
ON Authors.AuthorI D = OA_Link.OA_Name ID)
ON Entries.EntryID = OA_Link.OA_Entr yID)
INNER JOIN Authors AS Authors_1
ON Entries.AuthorI D = Authors_1.Autho rID

WHERE ((Authors.Autho rName LIKE "*twain*")
OR (Authors_1.Auth orName LIKE "*twain*"))

ORDER BY Entries.EntryID , Authors.AuthorN ame;

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.AuthorN ame
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.TitleSt atement, Authors_1.Autho rName AS
MainAuth, Authors.AuthorN ame AS AllAuth
FROM (Entries INNER JOIN (Authors INNER JOIN OA_Link ON Authors.AuthorI D
= OA_Link.OA_Name ID) ON Entries.EntryID = OA_Link.OA_Entr yID) INNER JOIN
Authors AS Authors_1 ON Entries.AuthorI D = Authors_1.Autho rID
WHERE (((Authors.Auth orName)="twain" ))
ORDER BY Entries.EntryID , Authors.AuthorN ame;

+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++
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.TitleSt atement, Authors.AuthorN ame
FROM Authors
INNER JOIN (Entries INNER JOIN OA_Link
ON Entries.EntryID = OA_Link.OA_Entr yID)
ON Authors.AuthorI D = OA_Link.OA_Name ID
WHERE Authors.AuthorN ame 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.TitleSt atement, Authors.AuthorN ame
FROM Authors INNER JOIN Entries
ON Authors.AuthorI D = Entries.AuthorI D
WHERE Authors.AuthorN ame 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
3220
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 references to functions and objects not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment." What I'm wondering is what exactly...
2
1301
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 ? It's the first time I've needed to do something like this. What I have is an application which was written (by me) with an Access backend (to run on a CD) and now it needs to run on SQL. However, I want to be able to swap over between backends so...
5
3413
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. There are 15 files created each month with each file containing specific data. What I started to do is store the files by year and then by month so I new when the data is based on. I have linked the table to the database and them append the...
11
4515
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 database,just help in getting me pointed in the right direction. I have a database with 8 tables, which from what I have read, cannot be linked on a single form, and be updatable. I have created a query which includes all 8 tables, and then...
1
2007
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 either link 3 tables together in a select query, and maintain the editablity that I get when linking two tables and creating a form. I know there is a way to link 3 tables through multiple queries however I have yet to be able to get it to work.
4
4661
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 the same field names in each table for this to work? What steps would I need to take in order to accomplish this?
2
2724
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 source pro 9. My application heavily uses RW tools through wrapper classes on the top existing RW classes. RW libraries were built using gcc on linux platform and standalong RW examples are working fine. Since the application compilation is moving...
7
3047
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.0;HDR=NO;IMEX=2;DATABASE=C:\Test It's really easy to connect to those tables in A97. I'm having difficulties in A2003. I'm trying to follow the instructions in http://support.microsoft.com/kb/824264/.
2
1195
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 have been sitting here for hours trying to link the data between several tables on 2 separate databases. I have a database which I use for keeping track of the status of documents from different dates. Every couple of weeks a new load of...
0
8382
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.