ITPub博客

首页 > 数据库 > MySQL > SQL位运算

SQL位运算

原创 MySQL 作者:壹頁書 时间:2015-12-28 19:58:59 0 删除 编辑
做网站的人都应该明白
有一些瓷,不可说,说不得,不可思,不可议.
但是,你懂的.

结构和数据:
  1. create table SENSITIVE_WORD(  
  2.     id int primary key auto_increment,  
  3.     word varchar(20) not null,  
  4.     module int  
  5. );  
  6.   
  7. insert into SENSITIVE_WORD(word) values('官窑');  
  8. insert into SENSITIVE_WORD(word) values('民窑');  
  9. insert into SENSITIVE_WORD(word) values('大宝剑');  
  10. commit;  

单位的敏感词库,同样的敏感词,不同的模块认定却不一样.
比如大宝剑,作为昵称输入,认为他是敏感词,直接过滤;但是内容的模块,聊天的模块却不认为是敏感词.

也可以考虑用SQL位运算的方式设计.
假设现在一共有如下模块,
昵称  2
内容  4
聊天  8
歌词  16
评论  32

  1. mysql> select id,power(2,id) from nums where id<=5;  
  2. +----+-------------+  
  3. | id | power(2,id) |  
  4. +----+-------------+  
  5. |  1 |           2 |  
  6. |  2 |           4 |  
  7. |  3 |           8 |  
  8. |  4 |          16 |  
  9. |  5 |          32 |  
  10. +----+-------------+  
  11. 5 rows in set (0.08 sec)  

每个模块代表一个二进制位,如果一个词在所有的模块都是敏感词,他的module (11111),转为10进制(2+4+8+16+32)=64

设置大宝剑在昵称,内容,歌词模块为敏感词
设置官窑在内容,聊天模块为敏感词
设置民窑在昵称,聊天模块为敏感词
  1. update SENSITIVE_WORD set module=(2+4+16) where word='大宝剑';  
  2. update SENSITIVE_WORD set module=(4+8) where word='官窑';  
  3. update SENSITIVE_WORD set module=(2+8) where word='民窑';  
  4.   
  5. mysql> select * from SENSITIVE_WORD;  
  6. +----+--------+--------+  
  7. | id | word   | module |  
  8. +----+--------+--------+  
  9. |  1 | 官窑   |     12 |  
  10. |  2 | 民窑   |     10 |  
  11. |  3 | 大宝剑 |     22 |  
  12. +----+--------+--------+  
  13. 3 rows in set (0.00 sec)  


应用在启动的时候,拉取昵称的敏感词.
  1. mysql> select * from SENSITIVE_WORD where module & 8=8;  
  2. +----+------+--------+  
  3. | id | word | module |  
  4. +----+------+--------+  
  5. |  1 | 官窑 |     12 |  
  6. |  2 | 民窑 |     10 |  
  7. +----+------+--------+  
  8. 2 rows in set (0.00 sec)  


这样的好处是
1.动态扩展模块而不需要修改表结构.否则每增加一个模块,就需要增加一个字段.
2.同时拉取多个模块的敏感词也比较简单,比如同时拉取昵称和歌词的敏感词
where module & (16+2) = (16+2)


MySQL相关二进制操作
  1. # BIN(N)返回二进制值N的一个字符串表示  
  2. mysql> select bin(123);  
  3. +----------+  
  4. | bin(123) |  
  5. +----------+  
  6. | 1111011  |  
  7. +----------+  
  8. 1 row in set (0.00 sec)  
  9.   
  10. # OCT(N)返回八进制值N的一个字符串表示  
  11. mysql> select oct(123);  
  12. +----------+  
  13. | oct(123) |  
  14. +----------+  
  15. | 173      |  
  16. +----------+  
  17. 1 row in set (0.00 sec)  
  18.   
  19. # HEX(N)返回十六进制值N的一个字符串表示  
  20. mysql> select hex(123);  
  21. +----------+  
  22. | hex(123) |  
  23. +----------+  
  24. | 7B       |  
  25. +----------+  
  26. 1 row in set (0.00 sec)  
  27.   
  28. mysql> select concat('0x', hex(123));  
  29. +------------------------+  
  30. | concat('0x', hex(123)) |  
  31. +------------------------+  
  32. | 0x7B                   |  
  33. +------------------------+  
  34. 1 row in set (0.00 sec)  

Oracle 有bitand函数

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1966195/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339948