我有一个列,我们存储所选日期值的总和..例如,我有固定的日期值
1 Sunday
2 Monday
4 Tuesday
8 Wednesday
16 Thursday
32 Friday
64 Saturday
在我的专栏中我得到了12,所以很清楚用户选择了(4)Tuesday+(8)Wednesday=12
现在我有106个逻辑我可以写出来找出它的天数组合吗?
答案 0 :(得分:2)
SELECT *,
( (SelDates & 1) = 1) AS IsSun,
( (SelDates & 2) = 2) AS IsMon,
( (SelDates & 4) = 4) AS IsTue,
( (SelDates & 8) = 8) AS IsWed,
( (SelDates & 16) = 16) AS IsThu,
( (SelDates & 32) = 32) AS IsFri,
( (SelDates & 64) = 64) AS IsSat
FROM <table>
这应该适用于MySQL和SQL-Server,因为它们都支持&amp;按位AND运算符。
答案 1 :(得分:0)
您可以像这样检查它(MS SQL,按位AND):
SELECT
CASE WHEN (daySum & 1) = 1 THEN 'IsMonday'
CASE WHEN (daySum & 2) = 2 THEN 'IsTuesDay'
CASE WHEN (daySum & 4) = 4 THEN 'IsWednesDay'
... etc ...
END FROM MyTable
答案 2 :(得分:0)
在MySQL中,您可以直接使用BIN
函数
SELECT LPAD(BIN(106), 7,'0') --> 1101010
MSB是星期六,LSB星期日