User parameters in another table: what is the best thing to check if there is an option?

I have several options that a user can have, mainly for checking their presence around the site.

The tables look like this:

Users

id=1
username=stackoverflow
password=oSKAJMMS;
address=xyz
...

      

Parameters:

user_id=1
option=AD3

      


user_id=1
option=AC1

      

At some point I need to check if it has a specific option (for example: AD3, AC1, etc.) in the "options" table, given that a user without an option simply does not exist in the "options" table.

What's the best practice for this? I, though about JOIN, but every time I read stackoverflow, I figure out simpler and more efficient methods, so I ask you what should I do!

0


a source to share


1 answer


SELECT  *
FROM    users u
WHERE   EXISTS (
        SELECT  1
        FROM    options o
        WHERE   o.user_id = u.id
                AND o.option = 'AC1'
        )

      



Make sure you have an index on options (user_id, option)

+2


a source







All Articles