
04-08-2007, 01:06 AM
|
Discordant
|
|
Join Date: Sep 2006
Location: Subsection 185.D354 C.12
Posts: 346
|
|
Wiki - SQL Commands
I'm putting this guide here so as to not lose it. This was a chat involving samandhi, ndnet, and myself. The chat involved SQL syntax and bitmask, which till now bitmasks were elusive to me. Bitmask work on interesting terms.
ndnet wrote the bitmask part, I already understood the basic idea, but this model really helps.
Example -
Code:
Select * From table;
Desc - This puts up all the tables, let assume one bitmask field. In this example there is only 5 entry's.
Code:
+------+
| mask |
+------+
| 5 | <--- 0101 = 0 + 4 + 0 + 1 = 5
| 2 | <--- 0010 = 0 + 0 + 2 + 0 = 2
| 3 | <--- 0011 = 0 + 0 + 2 + 1 = 3
| 8 | <--- 1000 = 8 + 0 + 0 + 0 = 8
| 4 | <--- 0100 = 0 + 4 + 0 + 0 = 4
+------+
Goal - I want every line where the number 4 (or 0100) is turned on, this would include values 4 and five 5 because 4 = 0100, and 5 = 0101. Both have the number 4 bit turned on.
Code:
Select * From table where mask & 4;
Result:
Code:
+------+
| mask |
+------+
| 5 | <--- 0101 = 0 + 4 + 0 + 1 = 5
| 4 | <--- 0100 = 0 + 4 + 0 + 0 = 4
+------+
But what if I don't want 4's? What if I want everything but 4's?
Code:
Select * from table where not mask & 4;
Code:
+------+
| mask |
+------+
| 2 | <--- 0010 = 0 + 0 + 2 + 0 = 2
| 3 | <--- 0011 = 0 + 0 + 2 + 1 = 3
| 8 | <--- 1000 = 8 + 0 + 0 + 0 = 8
+------+
And thats all about bitmaks, a list of operators;
Code:
BINARY Cast a string to a binary string
BIT_AND() Return bitwise and
BIT_COUNT() Return the number of bits that are set
BIT_LENGTH() Return length of argument in bits
BIT_OR() Return bitwise or
BIT_XOR() Return bitwise xor
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
/ Division operator
__________________
If at first you don't succeed destroy all evidence that you ever tried.
God doesn't give second chances... Hell, he sets you up the first time.
|