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 INNER JOIN help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-11-04, 09:24
kmallea kmallea is offline
Registered User
 
Join Date: Feb 2004
Posts: 1
SQL INNER JOIN help

I have a query where I need to get two names from a table. The names are on one project but have two seperate roles. In the previous db this want a problem because everything was in two tables. This one has 3 tables to relate this info

heres my sql

SELECT budgetAmount AS TrainingBudget, tp_project.projId AS ProjectNumber, courseDescription AS ProjectDescription, tp_proj_contact.firstName + ' ' + tp_proj_contact.lastName AS TrainingOwner, sponsorB.firstName + ' ' + sponsorB.lastName AS sponsorName
FROM tp_project
INNER JOIN tp_proj_contact ON tp_proj_contact.projID = tp_project.projID AND contactTypeId = 33
INNER JOIN tp_users ON hrid = contactHRID
LEFT OUTER JOIN tp_proj_contact AS sponsorA ON sponsorA.projID = tp_project.projID AND sponsorA.contactTypeId = 20
LEFT OUTER JOIN tp_users AS sponsorB ON sponsorB.hrid = sponsorA.contactHRID
WHERE tp_project.projID = 633

The differences are the contactTypeID one is 33 the other is 20, the error I get is

Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'firstName'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'lastName'.

Is it that I can't join the same table twice in one query? If so what alternatives do I have?
Reply With Quote
  #2 (permalink)  
Old 03-02-04, 09:35
DBrigham DBrigham is offline
Registered User
 
Join Date: Mar 2004
Location: Reston, Virginia
Posts: 7
Khary ...

Looking at your SQL statement quickly, two thoughts come to mind. First, and simplest: Is your system case sensitive? If so, I think I saw "ID" and "Id" mixed.

Second, I think you can do a "self-join" (joining the same table twice) by using an alias on the second attachment (you already are using AS in some of your field names).

Of course, mine is not expert advice, as you will see later in my posting. I have just started playing around with INNER JOINs. Microsoft documentation I have read discouraged OUTTER JOINs (LEFT and RIGHT) because they can cause problems by delivering too many records.
Reply With Quote
  #3 (permalink)  
Old 03-02-04, 09:54
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
If you use a table alias, then the table columns can only be referenced with the alias.

Example:

Select tableA.columnA
from tableA ta

This query will not work, instead the alias 'ta' must be used to reference 'columnA'.
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.

Last edited by r123456; 03-02-04 at 10:01.
Reply With Quote
  #4 (permalink)  
Old 03-03-04, 13:19
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Do both the tp_proj_contact and tp_users tables have columns named firstName and lastName?

-PatP
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