cnu
cnu

Reputation: 815

Join query in SQLite iPhone

I have two tables Visits and Labs
Visit has these fields: id,patientID
Labs has these fields: id, VisitID
And Visit.id = Labs.VisitID

I want to get all old lab reports of a patient. But in lab table I don't have patientID field, so for that I need to make use of visitID field, using which I can connect to Visit table and there I have patientID

So now using visitID(a common field) I want to find out all old lab reports of a patient

Now I want to get all labs of a patient (I know patientID)

Can anyone suggest me join query for this?

Upvotes: 1

Views: 3133

Answers (1)

Rob
Rob

Reputation: 437592

It's a simple INNER JOIN, right?

SELECT labs.* FROM labs INNER JOIN visit ON visit.id = labs.visitID AND patientID = ?

Or you can do a subselect:

SELECT labs.* FROM labs WHERE visitID IN (SELECT visit.id FROM visit WHERE patientID = ?)

Upvotes: 4

Related Questions