H SWAN
H SWAN

Reputation: 45

SQL query join not working

I have three tables.tooth,toothchart and tblpatient following are column names

tooth-Tid,toothName
toothchart-patientId,stage, TeethCode,note
tblpatient-patientId,fname

I want to get all the values from 'tooth' table and 'teethchart' table for a given patient values from 'tblpatient' table,but my query doesn't give all the values of tooth table.

Here is my query.

SELECT tooth.*,teethchart.*,fname 
FROM tooth 
LEFT JOIN teethchart ON tooth.toothName = teethchart.TeethCode 
left join tblpatient on teethchart.patientId=tblpatient.patientId 
where teethchart.patientId = 'P0001'

Can anyone help me to solve this problem?

Upvotes: 2

Views: 68

Answers (1)

TheBrokenSpoke
TheBrokenSpoke

Reputation: 381

The following query will pull all the items from the tooth chart, and only those matching from teethchart and tblpatient

SELECT t.*, p.fname, tc.*
From tooth t
left join teethchart tc on t.toothName = tc.TeethCode
left join tblpatient p on tc.patientId = p.patientId
and p.patientId = 'P0001';

Upvotes: 1

Related Questions