سؤال

I'm working with a table that has a field called other_group_ids that is of type varbinary(255). An example vale of it would be: 3,7,14. I have a query that checks to see if a user is in a certain group. For instance, to check if they're in group 4, I do a LIKE on it:

SELECT user.* FROM users AS user WHERE user.other_group_ids LIKE '%4%';

This works just fine, but the problem is that it's also catching on 14. How can I change my query to make it just match on 4, but not other values like 14 or 41?

Thank you very much for any insights!

هل كانت مفيدة؟

المحلول

To answer your question, you can do something like:

SELECT user.* FROM users AS user 
WHERE user.other_group_ids LIKE '%,4,%' OR
    user.other_group_ids LIKE '4,%' OR
    user.other_group_ids LIKE '%,4'

Be aware that having a column with multiple values is not normalized and you should try to fix this if possible. To normalize, create a table called user_group that has user and group. The primary key would be (user, group) and would act as a reference table. Then you can simply query the users that are in group 4.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top