3

Im have a database which saves user preferences in binary.

Chips = 1;
Pizza = 2;
Chinese = 4;

For example, if a user likes chips, then their preference will be 1. If they like pizza, their preference will be 2. If the like both, their preference will be 3. If they like chinese and chips, but not pizza, then their preference will be 5.

I need to create a mysql query where I can select all users who DO NOT like Pizza. I have attempted to solve this problem, but Im finding a lack of docs for using bit and in a where clause:

Logically, this makes sense to me, but this is not right:

SELECT * FROM `User` WHERE BIT_AND(preferences,2) != 2;

1 Answer 1

5
SELECT * FROM `User` 
WHERE preferences & 2 = 0

Source

SQLFiddle demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.