473,406 Members | 2,390 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,406 software developers and data experts.

Help trying to limit the results on a 3-table query?

I'm working on a .NET project and I need a single query to return a
result set from three related tables in Access 2003, and I'm having
trouble getting the results I want.

The details:

Question ------------
QuestionID
QuestionText
Question_MediaTypeID
etc.

Answer --------------
AnswerID
Answer_QuestionID
AnswerOptionText
etc.
AnswerMarket -----------
AMID
AM_MediaTypeID
AMmediaContent
etc.

The Question and Answer table are joined by the QuestionID and
Answer_QuestionID columns.
The Question and AnswerMarket tables are joined by the
Question_MediaTypeID and AM_MediaTypeID (they are both referring to
another table named MediaType, which describes whether we're talking
about newspapers, websites, or radio).

I need to pull back ONE specific record from the Question table (based
on the QuestionOrder parameter passed through a URL). No problem.

I need to pull back the THREE associated records in the Answer table:

SELECT *
FROM Question, Answer
WHERE QuestionOrder = @QuestionOrder AND
QuestionID = Answer_QuestionID

Works like a charm.

Now, there are EIGHTEEN rows in the AnswerMarket table. And, of
course, when I run this query I get 54 rows returned (3 x 18 = 54):

SELECT *
FROM Question, Answer, AnswerMarket_Chicago
WHERE
QuestionOrder = @QuestionOrder AND
QuestionID = Answer_QuestionID AND
Question_MediaTypeID = AM_MediaTypeID
I've tried a few techniques (including subqueries and different join
types), but I'm not getting the results I need.

Any suggestions and guidance (icluding best practices for this type of
situation) would be greatly appreciated.

Nov 16 '06 #1
2 2009
GH
If you are writing this query for Access or SQL Server, you might be
better off writing the query using the INNER JOIN syntax rather than
comma joins, as it is much easier to inadvertantly create a Cartesian
join using the latter. In your example, a JOIN query might look more
like:

SELECT * (<-- You really should itemize your select list rather than
using the wildcard.)
FROM (Question INNER JOIN Answer ON QuestionID = Answer_QuestionID)
INNER JOIN AnswerMarket_Chicago ON Question_MediaTypeID =
AM_MediaTypeID)
WHERE QuestionOrder = @QuestionOrder

The above join selects the answers for the selected question then the
media types for the question/answer records returned.

By the way, I am not sure why you are getting Cartesian results. I
duplicated your table structure and added some records, and I still get
back the correct result set with the query as you have written it.

- GH
mmitchell_houston wrote:
I'm working on a .NET project and I need a single query to return a
result set from three related tables in Access 2003, and I'm having
trouble getting the results I want.

The details:

Question ------------
QuestionID
QuestionText
Question_MediaTypeID
etc.

Answer --------------
AnswerID
Answer_QuestionID
AnswerOptionText
etc.
AnswerMarket -----------
AMID
AM_MediaTypeID
AMmediaContent
etc.

The Question and Answer table are joined by the QuestionID and
Answer_QuestionID columns.
The Question and AnswerMarket tables are joined by the
Question_MediaTypeID and AM_MediaTypeID (they are both referring to
another table named MediaType, which describes whether we're talking
about newspapers, websites, or radio).

I need to pull back ONE specific record from the Question table (based
on the QuestionOrder parameter passed through a URL). No problem.

I need to pull back the THREE associated records in the Answer table:

SELECT *
FROM Question, Answer
WHERE QuestionOrder = @QuestionOrder AND
QuestionID = Answer_QuestionID

Works like a charm.

Now, there are EIGHTEEN rows in the AnswerMarket table. And, of
course, when I run this query I get 54 rows returned (3 x 18 = 54):

SELECT *
FROM Question, Answer, AnswerMarket_Chicago
WHERE
QuestionOrder = @QuestionOrder AND
QuestionID = Answer_QuestionID AND
Question_MediaTypeID = AM_MediaTypeID
I've tried a few techniques (including subqueries and different join
types), but I'm not getting the results I need.

Any suggestions and guidance (icluding best practices for this type of
situation) would be greatly appreciated.
Nov 16 '06 #2
mmitchell_houston wrote:
I'm working on a .NET project and I need a single query to return a
result set from three related tables in Access 2003, and I'm having
trouble getting the results I want.
If this is my buddy from a place caled TMP, you're using the wrong join
syntax. 8)

Use the Access query builder... it's easy and quick and you dont have to
make your brain do somersaults figuring out how the from syntax goes...
You can copy the SQL from the query builder and use it in VBA or
whatever .net code you're writing.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Be Careful, Big Bird!" - Ditto "TIM-MAY!!" - Me
Nov 16 '06 #3

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

Similar topics

7
by: redneck_kiwi | last post by:
Need some opinions on how best to display in excess of 14K records based on users search criteria. Application Concept: ----------------------- User enters various search criteria (as many...
0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
1
by: Andrew | last post by:
Hi I am fairly new to python and was wondering if anyone out there could help me optomize this code. It is a simple Tkinter program that connects to a database and deletes records Some of...
15
by: chahnaz.ourzikene | last post by:
Hi all, This is the first i post in this newsgroup, i hope my english is not too bad... Let's get straight to the point ! I have a little probleme using threads in my little training example :...
3
by: Mahmood Ahmad | last post by:
Hello, I have written a program that reads three types of records, validates them acording to certain requirements and writes the valid records into a binary file. The invalid records are...
2
by: John Baker | last post by:
I find it highly annoying that MS Access tries to go online when I want to look at the help files. Is there a way to configure it so it just looks at my local helpfiles when I hit F1?
8
by: drose0927 | last post by:
Please help! I can't get my program to exit if the user hits the Escape button: When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this error: Parse Error, expecting `'}''...
9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
2
by: Joey | last post by:
I am currently developing a C# asp.net application where users are required to register. The application then generates a simple, plain text email and sends it to the new user. I have been...
33
by: insirawali | last post by:
I have written this in C#. I just need to convert it into C. da problem comes when handling the String. In C the String doesn't behave the same way as in C#. it would be great help if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
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.