Solved

Join three tables mySQL

Posted on 2011-03-11
2
304 Views
Last Modified: 2012-05-11
I want to join three tables: anestheticrecord, patient and anesthmedsgiven

anestheticrecord and patient share the field 'PatNum'
anestheticrecord and anesthmedsgiven share the field 'AnestheticRecordNum'

I've tried the following:

SELECT anestheticrecord.AnestheticRecordNum,anestheticrecord.AnestheticDate,
patient.PatNum as 'Patient #',patient.LName,patient.FName,patient.Gender,patient.Birthdate,patient.Address,patient.Address2,patient.City,patient.Zip,
patient.HmPhone,patient.WirelessPhone,anesthmedsgiven.AnesthMedName,anesthmedsgiven.QtyGiven,anesthmedsgiven.QtyWasted
FROM anestheticrecord,patient,anesthmedsgiven
WHERE ((anestheticrecord.PatNum = patient.PatNum) AND (anesthmedsgiven.AnestheticRecordNum = anestheticrecord.AnestheticRecordNum))
AND DATE_FORMAT(anestheticdate,'%Y-%m-%d') >= '2011-01-01' AND DATE_FORMAT(anestheticdate,'%Y-%m-%d') <= '2011-03-11'

which returns no results. I don't think I can use 2 LEFT JOINS because the three tables don't share a foreign key.

Any suggestions?
0
Comment
Question by:wjstarck
2 Comments
 
LVL 1

Accepted Solution

by:
DrivingInstructor earned 500 total points
Comment Utility
It is quite possible to use 2 left joins for this. The first left join uses the shared key between anestheticrecord and patient while the second left join uses the shared key between anestheticrecord and anesthmedsgiven thus:

 
SELECT * FROM anestheticrecord AS a
LEFT JOIN patient AS p (p.PatNum = a.PatNum)
LEFT JOIN anesthmedsgiven AS m (m.AnestheticRecordNum = a.AnestheticRecordNum)

Open in new window


May not seem real, and may break best practices, I don't know, but I know it can be done as I have done it.
0
 

Author Closing Comment

by:wjstarck
Comment Utility
Yes, that does work after all. Thanks.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now