做网站的人都应该明白
有一些瓷,不可说,说不得,不可思,不可议.
但是,你懂的.
结构和数据:
-
create table SENSITIVE_WORD(
-
id int primary key auto_increment,
-
word varchar(20) not null,
-
module int
-
);
-
-
insert into SENSITIVE_WORD(word) values('官窑');
-
insert into SENSITIVE_WORD(word) values('民窑');
-
insert into SENSITIVE_WORD(word) values('大宝剑');
-
commit;
单位的敏感词库,同样的敏感词,不同的模块认定却不一样.
比如大宝剑,作为昵称输入,认为他是敏感词,直接过滤;但是内容的模块,聊天的模块却不认为是敏感词.
也可以考虑用SQL位运算的方式设计.
假设现在一共有如下模块,
昵称 2
内容 4
聊天 8
歌词 16
评论 32
-
mysql> select id,power(2,id) from nums where id<=5;
-
+
-
| id | power(2,id) |
-
+
-
| 1 | 2 |
-
| 2 | 4 |
-
| 3 | 8 |
-
| 4 | 16 |
-
| 5 | 32 |
-
+
-
5 rows in set (0.08 sec)
每个模块代表一个二进制位,如果一个词在所有的模块都是敏感词,他的module (11111),转为10进制(2+4+8+16+32)=64
设置大宝剑在昵称,内容,歌词模块为敏感词
设置官窑在内容,聊天模块为敏感词
设置民窑在昵称,聊天模块为敏感词
-
update SENSITIVE_WORD set module=(2+4+16) where word='大宝剑';
-
update SENSITIVE_WORD set module=(4+8) where word='官窑';
-
update SENSITIVE_WORD set module=(2+8) where word='民窑';
-
-
mysql> select * from SENSITIVE_WORD;
-
+
-
| id | word | module |
-
+
-
| 1 | 官窑 | 12 |
-
| 2 | 民窑 | 10 |
-
| 3 | 大宝剑 | 22 |
-
+
-
3 rows in set (0.00 sec)
应用在启动的时候,拉取昵称的敏感词.
-
mysql> select * from SENSITIVE_WORD where module & 8=8;
-
+
-
| id | word | module |
-
+
-
| 1 | 官窑 | 12 |
-
| 2 | 民窑 | 10 |
-
+
-
2 rows in set (0.00 sec)
这样的好处是
1.动态扩展模块而不需要修改表结构.否则每增加一个模块,就需要增加一个字段.
2.同时拉取多个模块的敏感词也比较简单,比如同时拉取昵称和歌词的敏感词
where module & (16+2) = (16+2)
MySQL相关二进制操作
-
# BIN(N)返回二进制值N的一个字符串表示
-
mysql> select bin(123);
-
+
-
| bin(123) |
-
+
-
| 1111011 |
-
+
-
1 row in set (0.00 sec)
-
-
# OCT(N)返回八进制值N的一个字符串表示
-
mysql> select oct(123);
-
+
-
| oct(123) |
-
+
-
| 173 |
-
+
-
1 row in set (0.00 sec)
-
-
# HEX(N)返回十六进制值N的一个字符串表示
-
mysql> select hex(123);
-
+
-
| hex(123) |
-
+
-
| 7B |
-
+
-
1 row in set (0.00 sec)
-
-
mysql> select concat('0x', hex(123));
-
+
-
| concat('0x', hex(123)) |
-
+
-
| 0x7B |
-
+
-
1 row in set (0.00 sec)
Oracle 有bitand函数
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1966195/
,如需转载,请注明出处,否则将追究法律责任。