mysql set类型字段问题
PHP中文网
PHP中文网 2017-04-17 15:23:20
0
2
367

mysql设计的时候遇到一个多选项问题,搜索了一番似乎使用SET类型比较方便实现。

虽然弄明白了SET字段的存储及查询方法,但无奈能搜到的资料有限,还是有两个问题找不到头绪。

假设字段名为 flag ,SET的预设值有 A,B,C,D,E,F,G六个。

  1. 因为SET的预设值可能会增多,那么怎么能自动读出 SET的所有的预设值呢?

  2. 对于查询含有某个或者某几个值的时候可以使用:

    SELECT * FROM table WHERE FIND_IN_SET('A',flay);
    SELECT * FROM table WHERE FIND_IN_SET('A,C',flay); 类似的进行查询。
    
    但如果想要查询哪些包含一个选项(只有A或者B或者C……),哪些包含多个选项该怎么查询呢(AC,BCD什么的)?
PHP中文网
PHP中文网

认证0级讲师

reply all(2)
刘奇
  1. This is really disgusting. There are two methods, but neither of them are very good

    • desc table_name set_column_name

    • Go to information_schema and select
      Both methods have poor programmability. If I were to do it, I would probably create another table to hold the list of options

  2. There seems to be no good solution. You can use select char_length(set_column_name) from table_name; to see the number of characters and do it indirectly

洪涛
CREATE TABLE `table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `value` char(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Storing in this structure will be more flexible.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!