Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL selecting records help

Status
Not open for further replies.

kloner

Programmer
May 15, 2000
79
AU
SELECT E.Name, E.GroupClass, E.NIIN
FROM Equipment E
WHERE (E.Assessor LIKE 'te%') AND (E.ArchivedBy IS NULL OR
WHERE (E.Assessor LIKE 'te%') AND
(E.ArchivedBy IS NULL OR E.ArchivedBy = '') AND
(E.ArchivedByID IS NULL OR E.ArchivedByID = 0) AND
(E.ArchivedByPosID IS NULL OR E.ArchivedByPosID = 0) AND
(E.NIIN = EPN.NIIN)


Now I also need another field called "MRN" from a table called "EquipPartNumbers".
How do i display the "MRN" field in my results??

If i do :

SELECT E.Name, E.GroupClass, E.NIIN, EPN.MRN
FROM Equipment E
WHERE (E.Assessor LIKE 'te%') AND
(E.ArchivedBy IS NULL OR E.ArchivedBy = '') AND
(E.ArchivedByID IS NULL OR E.ArchivedByID = 0) AND
(E.ArchivedByPosID IS NULL OR E.ArchivedByPosID = 0) AND
(E.NIIN = EPN.NIIN)

Now the above returns all the MRN's where the NIIN is in both the Equipment
and EquipPartNumbers tables. But what if the NIIN doesn't exist in the Parts Table??
I still want the record to exist in my results with a Null/empty entry for that MRN.

Any ideas??




kloner
 
Use a LEFT JOIN or LEFT OUTER JOIN.

SELECT E.Name, E.GroupClass, E.NIIN, EPN.MRN
FROM Equipment E
LEFT JOIN quipPartNumbers EPN
ON (E.NIIN = EPN.NIIN)
WHERE (E.Assessor LIKE 'te%') AND
(E.ArchivedBy IS NULL OR E.ArchivedBy = '') AND
(E.ArchivedByID IS NULL OR E.ArchivedByID = 0) AND
(E.ArchivedByPosID IS NULL OR E.ArchivedByPosID = 0)
Terry L. Broadbent - DBA
SQL Server Page:
If you want to get the best answer for your question read faq183-874.
 
Thankyou for your help! That worked!

Can someone explain what the different JOIN's are, and what they do?

kloner
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top