我們如何將 MySQL SET 列作為整型偏移列表抓取?


我們可以藉助 MAKE_SET() 函式以整型偏移列表的形式抓取 MySQL SET 列值。為了便於理解,我們建立如下所示的名為“set_testing”的表 −

mysql> Create table set_testing( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, table SET('ABC','ABD','GHF') NOT NULL);
Query OK, 0 rows affected (0.08 sec)

mysql> Insert into set_testing (table) values('1');
Query OK, 1 row affected (0.06 sec)

mysql> Insert into set_testing (table) values('2');
Query OK, 1 row affected (0.06 sec)

mysql> Insert into set_testing (table) values('3');
Query OK, 1 row affected (0.02 sec)

mysql> Insert into set_testing (table) values('4');
Query OK, 1 row affected (0.02 sec)

mysql> Select * from set_testing;
+----+---------+
| id | table   |
+----+---------+
| 1  | ABC     |
| 2  | ABD     |
| 3  | ABC,ABD |
| 4  | GHF     |
+----+---------+
4 rows in set (0.00 sec)

現在,以下查詢將以整型偏移列表的形式抓取 MySQL SET 列 −

mysql> Select MAKE_SET(types+0,'1','2','3') as output from doctypes1;
+--------+
| output |
+--------+
| 1      |
| 2      |
| 1,2    |
| 3      |
+--------+
4 rows in set (0.00 sec)

更新於:22-06-2020

325 次瀏覽

開啟您的職業生涯

完成課程,獲得認證

立即開始
廣告
© . All rights reserved.