如何在 MySQL 中屏蔽数据字段?

mysqlmysqli database

要屏蔽数据字段,请使用 CONCAT() 和 REPEAT()。在这里,我们将使用 # 屏蔽数据字段。让我们首先创建一个 −

mysql> create table DemoTable1410
   -> (
   -> Password varchar(80)
   -> );
Query OK, 0 rows affected (0.51 sec)

使用 insert 在表中插入一些记录 −

mysql> insert into DemoTable1410 values('John12345678');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1410 values('Carol_897');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1410 values('David_5647383');
Query OK, 1 row affected (0.17 sec)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable1410;

这将产生以下输出 −

+---------------+
| Password      |
+---------------+
| John12345678  |
| Carol_897     |
| David_5647383 |
+---------------+
3 rows in set (0.00 sec)

这是在 MySQL 中屏蔽数据字段的查询 −

mysql> update DemoTable1410
   -> set Password=concat(substr(Password, 1, 5), repeat('#', char_length(Password) - 5));
Query OK, 3 rows affected (0.16 sec)
Rows matched: 3  Changed: 3 Warnings: 0

让我们再次检查表记录 −

mysql> select * from DemoTable1410;

这将产生以下输出 −

+---------------+
| Password      |
+---------------+
| John1#######  |
| Carol####     |
| David######## |
+---------------+
3 rows in set (0.00 sec)

相关文章