2012-01-05 80 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