mysql设计的时候遇到一个多选项问题,搜索了一番似乎使用SET类型比较方便实现。
虽然弄明白了SET字段的存储及查询方法,但无奈能搜到的资料有限,还是有两个问题找不到头绪。
假设字段名为 flag ,SET的预设值有 A,B,C,D,E,F,G六个。
因为SET的预设值可能会增多,那么怎么能自动读出 SET的所有的预设值呢?
对于查询含有某个或者某几个值的时候可以使用:
SELECT * FROM table WHERE FIND_IN_SET('A',flay);
SELECT * FROM table WHERE FIND_IN_SET('A,C',flay); 类似的进行查询。
但如果想要查询哪些包含一个选项(只有A或者B或者C……),哪些包含多个选项该怎么查询呢(AC,BCD什么的)?
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 selectBoth methods have poor programmability. If I were to do it, I would probably create another table to hold the list of options
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 indirectlyStoring in this structure will be more flexible.