我正在维护一个专注于销售服装的电子商务网站(在 LAMP 中)。
数据库最重要的表是服装表。
这张表有'服装名称'、'服装类型'、'服装款式'等字段
有些样式用数字表示。
例如,
1 = fashion 2 = cute 3 = sexy 4 = casual .. etc
每件衣服可以有多种款式(以逗号分隔),因此行可能如下所示:
'clothing_name' 'clothing_style'
Nice T-shirt 1,3,11,15
Old Jeans 1,2,8
Adida T-shirt 3,4,7
Nike T-shirt 3,4,7
每次我想显示一件衣服的款式时,我会做这样的事情:
<?php
/*
* $clothing['style'] is not usable
* so transform it into array first
*/
$style_numbers = explode(",", $clothing['style']);
$clothing['style_numbers'] = array();
if (!empty($clothing['style'])){
foreach ($style_numbers as $style_number){
$clothing['style_numbers'][] = (int) $style_number;
}
}
// and then mapping the numbers into its corresponding names
?>
这样,我得到一组样式,我可以用它显示样式名称。
一切都很好,直到我想实现“类似服装”功能
我不知道如何设置 SQL 子句来获取所有“可爱”的衣服(可爱的样式代码是 2)
我有两个问题
在这种情况下我如何实现“类似服装”功能?
以这种方式实现多对多关系(将所有值存储在以逗号分隔的一个字段中)是一种不好的做法吗?如果是这样,更好的解决方案是什么?
非常感谢您的任何建议。
非常感谢,我为我糟糕的英语感到抱歉。
最佳答案
我的建议是不要在表的任何列中存储字符串通知数组。为此使用多行。它增加了行数但使您的工作更轻松当您使用复杂查询时 类似的东西
'clothing_name' 'clothing_style'
Nice T-shirt 1
Nice T-shirt 3
Nice T-shirt 11
Nice T-shirt 15
Old Jeans 1
Old Jeans 2
Old Jeans 8
关于php - SQL 架构设计 : many-to-many relationship trouble while fetching data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21158562/