SQL: 如何将数据插入到 SET 数据类型中?

SQL: How to insert data into SET datatype?

如何插入 table SET 数据类型?

当我尝试插入 SET 数据时(在 MySQL 中)- 它成功插入(没有警告或错误)但我得到一个空单元格而不是一个由数据填充的单元格。

我这样试过:

INSERT INTO `individual_rate`(`user_id`, `movie_id`, `rate_code`, `rate_parts`) 
VALUES (3, 13,('1, 0, 0, 0, 0, 0'),('81, 102, 0, 102'))

也这样试过:

INSERT INTO `individual_rate`(`user_id`, `movie_id`, `rate_code`, `rate_parts`) 
VALUES (3, 13,'1, 0, 0, 0, 0, 0','81, 102, 0, 102')

它将数据插入到 user_idmovie_id,但没有为 rate_coderate_parts

插入数据

table结构:

CREATE TABLE `individual_rate` (
  `user_id` int(11) NOT NULL,
  `movie_id` int(11) NOT NULL,
  `rate_code` set('n','a','f','d','s','e') COLLATE utf8_unicode_ci NOT NULL COMMENT 'rate code',
  `rate_parts` set('top_f','bottom_f','top_a','bottom_a') COLLATE utf8_unicode_ci NOT NULL COMMENT 'generated parts'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

我做错了什么?

你可以使用show warnings;看看发生了什么。

这是结果,

show warnings;
+---------+------+-------------------------------------------------+
| Level   | Code | Message                                         |
+---------+------+-------------------------------------------------+
| Warning | 1265 | Data truncated for column 'rate_code' at row 1  |
| Warning | 1265 | Data truncated for column 'rate_parts' at row 1 |
+---------+------+-------------------------------------------------+

插入语句可以

 INSERT INTO `individual_rate`(`user_id`, `movie_id`, `rate_code`, `rate_parts`)  VALUES (3, 13, 'n', 'top_f');



  select * from individual_rate;
  1     +---------+----------+-----------+------------+
  2     | user_id | movie_id | rate_code | rate_parts |
  3     +---------+----------+-----------+------------+
  4     |       3 |       13 |           |            |
  5     |       3 |       13 | n         | top_f      |
  6     +---------+----------+-----------+------------+

在底层,它使用数字。实际上是位串中的位。 使用您定义 SET 字符串

另请参阅 ENUM,它也将字符串映射为数字。相反,ENUM 列只能设置为其中一个选项。可以为 SET 赋值的任意组合。