user1511924
user1511924

Reputation: 53

MYSQL bitwise OR over multiple entries

I need to update the status (bits) of a job depending on the status of all it's articles (also bits).

So for example I have a Job which has got the following articles

A: status = 1001
B: status = 1011
C: status = 0010

Now I want the job status to be = A OR B OR C = 1011.

Any way I can do this in MYSQL without having to make loops through all articles?

Thanks!

Upvotes: 0

Views: 1133

Answers (1)

Ian Atkin
Ian Atkin

Reputation: 6346

As with many problems, it's a simple case of reading the documentation: MySQL Bit Functions.

But let's get some things straight here. Are these values intended to be bitmasks? If that's the case (and I'm sure it is), then B is equal to A | C, which is a tad odd. Typically, bitmasks work in the following way...

A = 0001
B = 0010
C = 0100
...etc.

In this way, you can combine values and have the bitmask account for multiple options, so the combination of A and C (A | C) would be 0101.

The whole purpose of a bitmask is that they can represent multiple values in a fixed-length string, so they're space efficient. The downside is that they don't make for human-readable output, unless you're used to looking at The Matrix!

On the other hand, I could be way off.

But, to get you going, I believe you are looking for the BIT_OR function: MySQL GROUP BY (Aggregate) Functions, which calculates and returns the bitwise OR of all bits in expr.

Upvotes: 1

Related Questions