[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Join three tables mySQL

Posted on 2011-03-11
2
Medium Priority
?
314 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 2000 total points
ID: 35113940
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
ID: 35113994
Yes, that does work after all. Thanks.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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