Skip to content
Advertisement

How to query rows depending on multiple to-many properties?

I have one-to-many relation between tables user and tag:

My goal is to extract all users with tags java or java script only, but not users which have java, java script and C# together.

As output of the query I expect to receive following result:

I’ve tried to use query from SQL one-to-many relationship – How to SELECT rows depending on multiple to-many properties?, but as I noticed it is a bit different idea behind of it

Advertisement

Answer

One efficient, although lenghty to type, option uses exists:

With an index on tags(user_id, name), this should be very fast.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement