如何在 MySQL 中掩碼資料欄位?


如需掩碼資料欄位,請使用 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)

更新於: 2019-11-12

1K+瀏覽

開啟你的 事業

完成課程可獲得認證

開始
廣告