我有一个“t_form”表和一个“t_attribute”表。它看起来有点像下面这样。
表格
form_id | form_name | description
-----------------------------------
1 | Laptop | Possible attributes are Model, Screen Size, OS, Cd Player
2 | Mobile | Possible attributes are Model, OS
3 | Tablet | Possible attributes are Model, Screen size, OS
属性表
attribute_id | attribute
------------------------
1 | Model
2 | Screen Size
3 | OS
4 | Cd Player
- 笔记本电脑表单类型的可能属性包括型号、屏幕尺寸、操作系统、Cd 播放器。
- 移动表单类型的可能属性包括型号、操作系统。
- 平板电脑形式的可能属性包括型号、屏幕尺寸、操作系统。
Form_Attribute表
form_id | attribute_id
----------------------
1 | 1
1 | 2
1 | 3
1 | 4
2 | 2
2 | 2
3 | 2
3 | 2
3 | 2
我将根据用户从数据库中选择的表单类型(笔记本电脑、手机或平板电脑)生成如下 HTML 表单(带有表单字段)。
对于笔记本电脑:
对于移动设备:
对于平板电脑:
问题: 这是我的问题。
- 将用户输入的数据存储到数据库中的更好方法是什么。
- 从数据库中检索数据的更好方法是什么,因为我想实现过滤选项,例如:我想获取笔记本电脑屏幕尺寸从 14 到 16 可用的结果.
过滤器:
请提供一个可行的解决方案和一些示例,以便我更好地理解并帮助我实现这一目标。
最佳答案
我认为你可以选择像这样的 table
Form_Attribute_Values 或项目
item_id | form_id | attribute_id | value
------------------------------------------------
1 | 1 | 1 | SuperLap
1 | 1 | 2 | 15
1 | 1 | 3 | MS-DOS 6
1 | 1 | 4 | Toshiba
通过这种方式,您将能够查询输入的单个项目,还可以按表单、属性或属性值进行过滤。
您的 Form_Attribute 表将非常方便地生成特定表单的表单(即当用户想要输入笔记本电脑或移动电话时)。
项目表将是您在插入后存储项目或查询它们的位置。
您还可以在末尾添加一个时间戳列来存储项目的保存时间,如果您的数据将由多个用户填充,则还可以添加一个 User_id 列。如果您可以存储商店中没有的商品,或者您可以将可用性保留在单独的表中,则可能还需要一个附加列来显示可用性。
如果您可以从过滤表单中获取表单和属性 ID,则对屏幕尺寸为 14-16 的笔记本电脑的查询将如下所示:
SELECT item_id,
form_name,
attribute_name,
value
INNER JOIN form
ON item.form_id = form.form_id
INNER JOIN attribute
ON item.attribute_id = attribute.attribute_id
WHERE item_id IN (-- here I select for Laptop and Screen Size dimension
SELECT item_id
FROM item
WHERE form_id = 1
AND attribute_id = 2
AND item.value BETWEEN 14 and 16
)
否则你将需要一些更复杂的东西:
SELECT item_id
FROM item
INNER JOIN form
ON item.form_id = form.form_id
INNER JOIN attribute
ON item.attribute_id = attribute.attribute_id
WHERE item_id IN (-- here I select for Laptop and Screen Size dimension
SELECT item_id
FROM item
WHERE form_id = (-- but first I need the form_id
-- for Laptop
SELECT form_id
FROM form
WHERE form_name = 'Laptop')
AND attribute_id = (-- then I need the attribute_id
-- for Screen Size
SELECT attribute_id
FROM attribute
WHERE attribute_name = 'Screen Size')
AND item.value BETWEEN 14 and 16
)
关于mysql - 从数据库生成 HTML 表单并将值存储到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19541119/