473,399 Members | 3,832 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,399 software developers and data experts.

Query help with 2 tables plus one more - easy I think

I have two tables (tblRequest and tblDev) whose items have many-to-many
relationships with each other. I have set things up like this
(simplified):

Table 1 fields: tblRequest.PrimaryKey tblRequest.Description
example: 10 45 Elm
11 63 Green
12 123 Main Street
13 23 Pine

Table 2 fields: tblDev.PrimaryKey tblDev.Description
example: 20 Local. No issues
21 City owned and main
22 Out of county
23 Not seen by officer

Then Table 3 keeps track of the relationships between tables 1 and 2 by
tracking the primary key of the linked items in tables 1 and 2.
Table 3 fields: tblLink.RequestPrimaryKey tblLink.DevPrimaryKey
10 20
10 21
11 22
12 22

Items from tables 1 and 2 may or may not have a relationship listed in
table 3.

So given an item in tblRequest:
tblRequest.PrimaryKey tblRequest.Description
12 123 Main Street

I need two queries...
The first query would return ALL items from tblDev that are not
associated with this item in tblRequest - in this case 20, 21, 23.

The second query would return ALL items from tblDev that ARE associated
with this item in tblRequest - in this case 22.

That's it. Thanks in advance

Jul 23 '05 #1
1 1139
For future posts you'll find that a few lines of DDL can save you a lot
of typing and will make things a lot clearer for others. Here's how I
understand your table structure from your narrative:

CREATE TABLE Request (request_no INTEGER PRIMARY KEY, description
VARCHAR(20) NOT NULL UNIQUE)

CREATE TABLE Dev (dev_no INTEGER PRIMARY KEY, description VARCHAR(20)
NOT NULL UNIQUE)

CREATE TABLE Link /* Meaningless name for a table! */ (request_no
INTEGER NOT NULL REFERENCES Request (request_no), dev_no INTEGER NOT
NULL REFERENCES Dev (dev_no), PRIMARY KEY (request_no,dev_no))

(I dropped "tbl". Hungarian notation is generally considered bad form
in a database schema.)

Here are the two queries you asked for:

SELECT D.dev_no, D.description
FROM Dev AS D
LEFT JOIN Link AS J
ON D.dev_no = J.dev_no
AND J.request_no = 12
WHERE J.dev_no IS NULL

SELECT D.dev_no, D.description
FROM Dev AS D
JOIN Link AS J
ON D.dev_no = J.dev_no
AND J.request_no = 12

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2

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

Similar topics

5
by: Muhd | last post by:
To start off i have a database table that consists of "profiles", another that consists of "users" and finally one that consists of "exclusions", these are defined in the DDL below. I am trying to...
6
by: Jeremy Wallace | last post by:
I have a ton of queries that I need users to be able to view. I'd like to have them viewed in a datasheet-view form instead of directly, so that I can keep the users from futzing with the data. ...
8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
4
by: Andy_Khosravi | last post by:
I'm trying to build a search utility for users to find 'inquiries' in my database that involves several tables. This is normally easy to do with the query builder, but I have a unique situation...
5
by: Wired Hosting News | last post by:
I tried to be breif and give a scenario so as not to be overlooked because it was soooo long. Let me give you real world. I am a manufacturer of goods and produce 11 items that are distributed...
5
by: jonceramic | last post by:
Hi All, I started developing in Access, and people took notice and so we're starting to migrate into our corporate's bigger Oracle system. I'll still be using my developed Access front ends,...
3
by: santoni | last post by:
I really need help me with this. I'm sure it's really easy for most. I really appreciate any help. Thanks! I have tables Employee and Sales. In Employees the yes/no field IsManager denotes...
3
by: Andrew | last post by:
Hi All I wonder if anyone can help me with this. The scenario is that I have a pair of related tables. One contains record labels, the other contains contact names at those labels. In the...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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.