lundi 22 décembre 2014

sqlite left join with three tables

I have 3 tables


Tenants:_id, Firstname..etc


Tenancy:_id,tenant_id(FK). and others


TenantMeters:_id,tenant_id(FK) and other fields.


I want to construct a query which returns records in two cases:


(1) There is a record for a tenant in both tables(Tenancy and TenantMeters)


(2) There is a record for a tenant in either Tenancy table or TenantMeters


Any suggestions?


I have tried



SELECT Tenants._id ,TenantMeters.Tenant_id, Tenancy.tenant_id from Tenancy left join Tenants on Tenants._id=Tenancy.tenant_id left join TenantMeters on Tenants._id=TenantMeters .Tenant_id where TenantMeters.Tenant_id=? Or Tenancy.tenant_id=?


But fails in case where i have a record TenantMeters table only.


Aucun commentaire:

Enregistrer un commentaire