MySQL, how would my function to evalue a number look?
I need a function in MySQL which will eva开发者_开发百科luate a status code...
In my program this is how I evaluate it. However, in SQL I'd want to select all the status code which had a specific Reason. You can see below the reasons and that more than one can be set.
Sub InterpretReasonCode(ByVal pintCode As Integer)
If pintCode >= 16 Then
pintCode -= 16
mbooBlacklistedDomain = True
End If
If pintCode >= 8 Then
pintCode -= 8
mbooSneakedURLChanged = True
End If
If pintCode >= 4 Then
pintCode -= 4
mbooRetriedFailedToAccess = True
End If
If pintCode >= 2 Then
pintCode -= 2
mbooRequestedByAuthor = True
End If
If pintCode >= 1 Then
pintCode -= 1
mbooBlackListed = True
End If
End Sub
My SQL statement would look something like this
Select * from MyTable where Eval_Func(StatusCode,8) = true;
In the future I will be expanding the function to include different flags, .e.g 32, 64, 128, 256 etc.
You don't need a function. You can mask it directly using bitwise operators
For example:
-- Get all BlacklistedDomain
select *
from tab
where reason_bit_mask & 16;
And I advice against this on anything other than really small tables, because you can't index it properly.
精彩评论