473,785 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Three tables?

MEM
hello,

I'm need some help with some sql to return some data...obviousl y ;)

I have 3 tables. tblMgr, tblSup, and tblRep. I'd like to get all the
names of the reps that are assigned to a particular manager.

Here is a simplified exmaple of table setup:

tblRep
-------
repid repname supid
----------------------
1 joe 500
2 ann 501
tblSup
-------
supid supname mgrid
----------------------
500 bill 1000
502 tom 1001
tblMgr
-------
mgrid mgrname
---------------
1000 andy
1001 roger

Thanks.
Jul 20 '05 #1
3 72941
"MEM" <mm*****@citlin k.net> wrote in message news:94******** *************** ***@posting.goo gle.com...
hello,

I'm need some help with some sql to return some data...obviousl y ;)

I have 3 tables. tblMgr, tblSup, and tblRep. I'd like to get all the
names of the reps that are assigned to a particular manager.

Here is a simplified exmaple of table setup:

tblRep
-------
repid repname supid
----------------------
1 joe 500
2 ann 501
tblSup
-------
supid supname mgrid
----------------------
500 bill 1000
502 tom 1001
tblMgr
-------
mgrid mgrname
---------------
1000 andy
1001 roger

Thanks.


CREATE VIEW RepsManagers (repid, repname, mgrid, mgrname)
AS
SELECT R.repid, R.repname, M.mgrid, M.mgrname
FROM tblRep AS R
INNER JOIN
tblSup AS S
ON R.supid = S.supid
INNER JOIN
tblMgr AS M
ON S.mgrid = M.mgrid

-- For example
SELECT *
FROM RepsManagers
WHERE mgrname = 'andy'

Regards,
jag
Jul 20 '05 #2
"MEM" <mm*****@citlin k.net> wrote in message
news:94******** *************** ***@posting.goo gle.com...
hello,

I'm need some help with some sql to return some data...obviousl y ;)

I have 3 tables. tblMgr, tblSup, and tblRep. I'd like to get all the
names of the reps that are assigned to a particular manager.


You didn't specify if you wanted all managers (even if they don't have any
reps assigned)... If this is the case, you'll need to use an outer join...
This script creates tables in tempdb... Hope this helps...

use tempdb
go
drop table tblRep
drop table tblSup
drop table tblMgr

go
create table tblRep (repid int, repname char(5), supid int)
create table tblSup (supid int, supname char(5), mgrid int)
create table tblMgr (mgrid int, mgrname char(5))
go

insert into tblRep values (1,'name1', 1)
insert into tblRep values (2,'name2', 1)

insert into tblSup values (1,'sup1',10)
insert into tblSup values (2,'sup2',10)
insert into tblSup values (4,'sup4',20)

insert into tblMgr values (1, 'mgr1')
insert into tblMgr values (10, 'mgr10')
insert into tblMgr values (20, 'mgr20')
go

select
m.mgrid, m.mgrname, r.repid, r.repname
from tblMgr m
left outer join tblSup s on s.mgrid = m.mgrid
left outer join tblRep r on r.supid = s.supid
go
Jul 20 '05 #3
"MEM" <mm*****@citlin k.net> wrote in message
news:94******** *************** ***@posting.goo gle.com...
hello,

I'm need some help with some sql to return some data...obviousl y ;)

I have 3 tables. tblMgr, tblSup, and tblRep. I'd like to get all the
names of the reps that are assigned to a particular manager.


You didn't specify if you wanted all managers (even if they don't have any
reps assigned)... If this is the case, you'll need to use an outer join...
This script creates tables in tempdb... Hope this helps...

use tempdb
go
drop table tblRep
drop table tblSup
drop table tblMgr

go
create table tblRep (repid int, repname char(5), supid int)
create table tblSup (supid int, supname char(5), mgrid int)
create table tblMgr (mgrid int, mgrname char(5))
go

insert into tblRep values (1,'name1', 1)
insert into tblRep values (2,'name2', 1)

insert into tblSup values (1,'sup1',10)
insert into tblSup values (2,'sup2',10)
insert into tblSup values (4,'sup4',20)

insert into tblMgr values (1, 'mgr1')
insert into tblMgr values (10, 'mgr10')
insert into tblMgr values (20, 'mgr20')
go

select
m.mgrid, m.mgrname, r.repid, r.repname
from tblMgr m
left outer join tblSup s on s.mgrid = m.mgrid
left outer join tblRep r on r.supid = s.supid
go
Jul 20 '05 #4

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

Similar topics

5
3267
by: David Logan | last post by:
Hello, I am trying to construct a query across 5 tables but primarily 3 tables. Plan, Provider, ProviderLocation are the three primary tables the other tables are lookup tables for values the other tables. PlanID is the primary in Plan and Plan Provider ProviderLocation Lookups -------- ---------- ---------------- ----------- PlanID ProviderID ProviderID LookupType
7
6237
by: alexcn | last post by:
I have the following query: SELECT dbo.tSymExch.exCode, dbo.tSymGrp.sgCode, dbo.tSymMain.smCode FROM dbo.tSymExch FULL OUTER JOIN dbo.tSymGrp ON dbo.tSymExch.exID = dbo.tSymGrp.sgexID FULL OUTER JOIN dbo.tSymMain ON dbo.tSymGrp.sgID = dbo.tSymMain.smsgID Which produces:
1
16654
by: TeleTech1212 | last post by:
I am trying to select specific columns from multiple tables based on a common identifier found in each table. For example, the three tables: PUBACC_AC PUBACC_AM PUBACC_AN each have a common column:
2
2674
by: terence.parker | last post by:
I am often faced with the dilemma of whether to use a JOIN query across three tables in order to grab a bunch of results - or whether to create another table to represent what I want. The latter is less normalised, but seems less computationally expensive to me(?). Basicially what I have is: Friend (uid1,uid2,fid) -> FriendshipCategories(fid,cid) -> Categories(cid,name) If I want to find out all the 'categories' (names) which a user...
4
23913
by: Nathan | last post by:
I have an application that uses an Access database to gather information on students' test scores. In the database there are three tables which are joined by one- to-many relationships: Students, Subjects, and Tests. I am trying to create a query that joins these three tables and show all the subjects for only one student, and all the tests taken in each of those subjects. This is the query I have entered in the Query Builder: ...
4
1822
by: Jack | last post by:
Hi all, While debugging some old code from someone, I came across this stored procedure: SELECT dbo.TBL_COORD.COORD_ID AS ID, dbo.TBL_COORD.LATITUDE AS Latitude, dbo.TBL_COORD.LONGITUDE AS Longitude, dbo.TBL_COORD.NORTHING AS Northing, dbo.TBL_COORD.EASTING AS Easting, dbo.refDROP_VALUES.Drop_Value AS , refDROP_VALUES_1.Drop_Value AS , refDROP_VALUES_2.Drop_Value AS
5
2003
by: john7 | last post by:
I have three tables one for categories, one for products1 and another for products2. I am using PHP and MySQL. so for example I categories categoryid name 1 programming 2 databases 3 os
3
16507
by: Zeff | last post by:
Hi all, I have a relational database, where all info is kept in separate tables and just the id's from those tables are stored in one central table (tblMaster)... I want to perform a query, so all data from the separate tables is shown in a view (instead of the reference id's pointing to the separate tables...) I have some troubles formulating the SQL statement: I tried:
0
1006
by: piglet | last post by:
Hi! I try to pull out data from three tables, and face a problem. Here is my query: SELECT Census2000_1.GEO2000, .FIPS, SUM(2003_Full].DEPSUMBR) AS TOTAL, V032006.GEO2000 AS V032006 FROM FULL OUTER JOIN Census2000_1 ON FDIC.dbo..FIPS = Census2000_1.GEO2000
1
2221
by: Vivienne | last post by:
Hi there This is a hard problem that I have - I have only been using sql for a couple of weeks and have gone past my ability level quickly! The real tables are complex but I will post a simple and a real version with the hope someone can help me. Any help would be much appreciated - I would also be happy to pay someone to actually do it if it takes time to work out as I know that its hard when all your help is free :)...
0
9643
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
9480
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
10147
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
10087
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
9947
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
7496
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
6737
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();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.