简体   繁体   English

如何设计表架构以存储来自的值<select multiple>

[英]How to design table schema for storing values from <select multiple>

I have a select-multiple list html control, which enables end user to select multiple items. 我有一个select-multiple list html控件,它使最终用户可以选择多个项目。 Currently, I stored user selected values as "truck, heavy truck, others" into mysql varchar field. 目前,我将用户选择的值(例如“卡车,重型卡车,其他”)存储到mysql varchar字段中。 But both truck and heavy truck are searched out if user select truck on select multiple control. 但是,如果用户在选择多个控件上选择卡车,则将同时搜索卡车和重型卡车。 SQL statement for this query is like "select truckType from table where trackType like '%truck%'". 此查询的SQL语句类似于“从表中选择truckType,其中trackType如'%truck%'”。

Seems the only way for the solution is to put selected values into another table. 解决方案的唯一方法似乎是将选定的值放入另一个表中。

Is there any other way to handle this issue in one table? 还有其他方法可以在一个表中处理此问题吗?

add , to the end and start also 添加,最后也开始

so value becomes , truck, heavy truck, others, 所以价值变成了, truck, heavy truck, others,

use this query 使用此查询

select truckType from table where trackType like '%, truck,%

NOTE: , in like '%, truck,%' 注意: , like '%, truck,%'

you should make a separate table: 您应该制作一个单独的表:

------------------
typeId | type        |
-----------------
1      | truck       |
2      | heavy truck |
....

Also table for entity: 实体表:

------------------
entityid |         |
-----------------
1        | entity1 |
2        | entity2 |

and other table will have multiple to one relationship with this table: 而其他表将与此表具有多对一关系:

-------------------------------
id | typeId      | entityId    |
-------------------------------
1  | 1           | 2           |
2  | 2           | 2           |
2  | 2           | 1           |

And your select would be a join of this two tables. 您的选择将是这两个表的联接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM