Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

วิธีดึงข้อมูลคอลัมน์ SET ใน MySQL เป็นรายการออฟเซ็ตจำนวนเต็ม


เราสามารถดึงค่าคอลัมน์ SET ของ MySQL เป็นรายการออฟเซ็ตจำนวนเต็มได้ด้วยฟังก์ชัน MAKE_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)

คำสั่งต่อไปนี้จะดึงคอลัมน์ SET ของ MySQL เป็นรายการออฟเซ็ตจำนวนเต็ม:

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)