samedi 9 janvier 2016

SQLite conditional where clause

I have three tables

ZoneTable
ID, Name

UserTable
ID, Name

UserZone
ZoneTableID, UserTableID

Now.

SELECT ID, Name from ZoneTable

gives me the full list of zones.

If i do the following

SELECT ZoneTable.ID, ZoneTable.Name from ZoneTable LEFT JOIN UserZone on UserZone.ZoneTableID = ZoneTable.ID WHERE UserTableID = :ID GROUP BY ZoneTable.ID

gives me the list of zones the user is allowed in.

Now to make things annoying if a user is allowed in all zones then they don't appear in the UserZone at all.

So is there anyway i can get the list of ZoneTableID's per specific user user, if the count is 0 then don't apply the where clause, if count > 0 then only show the specific zone names, all in one query.

If that makes sense

Thanks

Aucun commentaire:

Enregistrer un commentaire