If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL Query Question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-05-03, 19:27
cmptrguru7 cmptrguru7 is offline
Registered User
 
Join Date: Feb 2002
Posts: 43
SQL Query Question

I am hoping someone can help me. I am trying to do a query to gather some information. My database is set up like:

person table:

Person ID
Last Name
First Name
...

Customer table:

Cust_ID (foreign key links to Person ID)
Location
Employee ID (foreign key links to employee table)
...

Employee Table:

Employee ID (foreign key links to Person ID)
....

there is other information in the tables, but this is enough to get what I want. I am trying to write a query to give me the employee name and which customers coorespond to that employee. I need both the employee and customer names as well as the csutomer location. Does anyone have an idea on how to accomplish this.
Reply With Quote
  #2 (permalink)  
Old 09-08-03, 05:50
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: SQL Query Question

Quote:
Originally posted by cmptrguru7
I am hoping someone can help me. I am trying to do a query to gather some information. My database is set up like:

person table:

Person ID
Last Name
First Name
...

Customer table:

Cust_ID (foreign key links to Person ID)
Location
Employee ID (foreign key links to employee table)
...

Employee Table:

Employee ID (foreign key links to Person ID)
....

there is other information in the tables, but this is enough to get what I want. I am trying to write a query to give me the employee name and which customers coorespond to that employee. I need both the employee and customer names as well as the csutomer location. Does anyone have an idea on how to accomplish this.
Something like:

select p.last_name, c.cust_name, c.location
from person p, employee e, customer c
where p.person_id = e.person_id
and e.employee_id = c.employee_id
order by p.last_name, c.cust_name;
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 09-08-03, 12:30
cmptrguru7 cmptrguru7 is offline
Registered User
 
Join Date: Feb 2002
Posts: 43
Re: SQL Query Question

Quote:
Originally posted by andrewst
Something like:

select p.last_name, c.cust_name, c.location
from person p, employee e, customer c
where p.person_id = e.person_id
and e.employee_id = c.employee_id
order by p.last_name, c.cust_name;
The customer table does not have a customer name. All information about a person is stored in the person table. I am not sure how I can get what I need from this query. I think I am going to have to do a subquery somewhere.
Reply With Quote
  #4 (permalink)  
Old 09-08-03, 12:39
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: SQL Query Question

Right, didn't spot this part:

Cust_ID (foreign key links to Person ID)

You just need to join to Person table again:

select p.last_name, p2.last_name as cust_name, c.location
from person p, employee e, customer c, person p2
where p.person_id = e.person_id
and e.employee_id = c.employee_id
and p2.person_id= c.cust_id
order by p.last_name, p2.last_name;
__________________
Tony Andrews
http://tinyurl.com/tonyandrews

Last edited by andrewst; 09-08-03 at 12:45.
Reply With Quote
  #5 (permalink)  
Old 09-08-03, 14:28
cmptrguru7 cmptrguru7 is offline
Registered User
 
Join Date: Feb 2002
Posts: 43
Re: SQL Query Question

Quote:
Originally posted by andrewst
Right, didn't spot this part:

Cust_ID (foreign key links to Person ID)

You just need to join to Person table again:

select p.last_name, p2.last_name as cust_name, c.location
from person p, employee e, customer c, person p2
where p.person_id = e.person_id
and e.employee_id = c.employee_id
and p2.person_id= c.cust_id
order by p.last_name, p2.last_name;
Thank you that worked!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On