2012-01-05 82 views
0

我在我的数据库中的一个字段:xfield
此领域有我的职位(产品)在网站的格式如下:制作高级搜索

weight|3kg|year|2009|brand|samsung|monitorsize|13"|modem|yes 

现在我想执行的高级搜索。例如,我想搜索13“〜15”
和重量1.5kg〜3.5kg之间的显示器尺寸

如何使用php制作该搜索页?

+0

什么数据类型是2列?即时猜测他们是varchar或文本? – ManseUK 2012-01-05 09:33:05

+4

如果你想搜索它,我建议你将这些数据分成多列。否则,搜索它会很慢并且不那么容易。 – 2012-01-05 09:36:01

+0

固定格式列不适合在变量范围内查询。创建一个视图,将其再次扩展为key:value表。 – mario 2012-01-05 09:37:20

回答

1

您在数据库中使用CSV数据,这实在是一个非常糟糕的主意,它会让您变得粗体(插入拔出头发的人的随机图片)。
千万不要在数据库中使用CSV,它是一种反模式。

取而代之,您需要做的是重构您的数据库设计,只将一个值放在一个字段中。
我猜你想尽可能灵活。
在这种情况下,使用entity-attribute-value模型(EAV)。

你的表应该是这样的:

table properties (

id unsigned integer auto_increment primary key, 
product_id unsigned integer, 
attribute varchar(20) not null,  
astring varchar(100), 
anumber decimal(10,2), 
atype enum('string','integer','boolean','float'), 
unit varchar(10) not null, 
foreign key (product_id) references product(id) on update cascade on delete cascade, 
foreign key (attribute) references attribute(name) on update cascade on delete cascade, 
foreign key (unit) references unit(name) on update cascade on delete cascade 
) ENGINE = InnoDB; 

table unit (
name varchar(10) primary key -- "kg","inch",....... 
) ENGINE = InnoDB; 

table attribute (
name varchar(20) primary key -- allowed name for the attribute 
) ENGINE = InnoDB; 

到外部表的链接确保你的单位和属性从一致的标识符的有限池选择。

现在你可以使用这样的查询您的数据库:

SELECT p.id, p.name 
FROM product p 
INNER JOIN properties ps1 ON (ps1.product_id = p.id) 
INNER JOIN properties ps2 ON (ps2.product_id = p.id) 
WHERE ps1.attribute = 'monitorsize' AND ps1.anumber BETWEEN 13 AND 15 
WHERE ps2.attribute = 'weight' AND ps2.anumber BETWEEN 1.5 AND 3.5 
+0

我使用datalifeengine cms,我真的不能编辑此cms的xfield部门。 有没有办法? 感谢以上答案。 – ArlopA 2012-01-05 19:56:17