需要设计数据库的指导 Table -(混淆 1 列)
Need Guidance for designing database Table - (confuse for 1 column)
我有两个 table。 1.Products 2。组合
产品 Table 的列类似于 (product_id, product_name).
Combo Table has column like (combo_id, combo_name, combo_included_products /* combo 可能有 2 个或更多来自产品 table*/)
的产品
规则:1个组合可以有多个产品。
Product Table
product_id product_name
1 Pen
2 Pencil
3 Pen Holders
4 Sharpeners
-
Combo Table
combo_id combo_name this_combo_includes_below_products
1 Big_combo (1,2,3,4) => big combo includes all products
2 Small_combo (2,4) => this combo only includes product 2,4
3 test_combo (1,2)
4 Other_combo (1,4)
那么如何在组合的第三列中插入多个产品 ID table?
我正在考虑存储 1,2,3,4 和 2,4 等数据。然后问题将是编写连接查询。即
select combo.combo_id, combo.combo_name, product.product.id from combo join product ON combo.this_combo_included_products_id = product.product_id <= 因为会有多个产品id,所以不可能。
我也在考虑制作一个脚本,我将首先按原样获取组合 table,然后我将第三列按“,”拆分,然后 运行 迭代 (select * from combo where product id=this_combo_included_item_id[i]) <= 我不确定这是个好主意,但这可能是一个替代解决方案,之后需要一些编码。
(无论如何我都使用 phpmysql 来获取数据 - 所以我可以在获取后处理它。)
$sql = "SELECT * FROM combo";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
// I can run other query here
$child_query = "select combo.combo_id, combo.combo_name, product.product.id from combo join product
ON combo.this_combo_included_products_id = product.product_id";
}
但是在设计数据库时我还能做些什么吗table。谢谢。
不要在一行中存储多个值。不要将数字存储为字符串。对 famous SO question 的公认答案提供了关于这有多糟糕的深刻见解。
产品和组合之间存在多对多关系:每个产品可能出现在多个组合中,每个组合可能包含多个产品。从规范化的角度来看,表示它的正确方法是创建另一个 table,称为桥 table,来存储关系。
create table product_combo (
product_id int references product(product_id),
combo_id int references combo(combo_id),
primary key (product_id, combo_id)
);
对于您的示例数据,网桥 table 将包含:
product_id combo_id
1 1
1 2
1 3
1 4
2 2
2 4
3 1
3 2
4 1
4 4
有了这个设置,假设您想要 select 给定的组合及其所有相关产品,那么您会去:
select c.*, p.*
from combos c
inner join product_combos pc on pc.combo_id = c.combo_id
inner join products p on p.product_id = pc.product_id
where c.combo_id = ?
如果您真的想要,您甚至可以为每个组合重建产品的 csv 列表:
select c.combo_id, c.combo_name, group_concat(p.product_name) product_names
from products p
inner join product_combos pc on pc.product_id = p.product_id
inner jon combos c on c.combo_id = pc.combo_id
group by c.combo_id, c.combo_name
我有两个 table。 1.Products 2。组合
产品 Table 的列类似于 (product_id, product_name).
Combo Table has column like (combo_id, combo_name, combo_included_products /* combo 可能有 2 个或更多来自产品 table*/)
的产品规则:1个组合可以有多个产品。
Product Table
product_id product_name
1 Pen
2 Pencil
3 Pen Holders
4 Sharpeners
-
Combo Table
combo_id combo_name this_combo_includes_below_products
1 Big_combo (1,2,3,4) => big combo includes all products
2 Small_combo (2,4) => this combo only includes product 2,4
3 test_combo (1,2)
4 Other_combo (1,4)
那么如何在组合的第三列中插入多个产品 ID table?
我正在考虑存储 1,2,3,4 和 2,4 等数据。然后问题将是编写连接查询。即
select combo.combo_id, combo.combo_name, product.product.id from combo join product ON combo.this_combo_included_products_id = product.product_id <= 因为会有多个产品id,所以不可能。
我也在考虑制作一个脚本,我将首先按原样获取组合 table,然后我将第三列按“,”拆分,然后 运行 迭代 (select * from combo where product id=this_combo_included_item_id[i]) <= 我不确定这是个好主意,但这可能是一个替代解决方案,之后需要一些编码。 (无论如何我都使用 phpmysql 来获取数据 - 所以我可以在获取后处理它。)
$sql = "SELECT * FROM combo";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
// I can run other query here
$child_query = "select combo.combo_id, combo.combo_name, product.product.id from combo join product
ON combo.this_combo_included_products_id = product.product_id";
}
但是在设计数据库时我还能做些什么吗table。谢谢。
不要在一行中存储多个值。不要将数字存储为字符串。对 famous SO question 的公认答案提供了关于这有多糟糕的深刻见解。
产品和组合之间存在多对多关系:每个产品可能出现在多个组合中,每个组合可能包含多个产品。从规范化的角度来看,表示它的正确方法是创建另一个 table,称为桥 table,来存储关系。
create table product_combo (
product_id int references product(product_id),
combo_id int references combo(combo_id),
primary key (product_id, combo_id)
);
对于您的示例数据,网桥 table 将包含:
product_id combo_id
1 1
1 2
1 3
1 4
2 2
2 4
3 1
3 2
4 1
4 4
有了这个设置,假设您想要 select 给定的组合及其所有相关产品,那么您会去:
select c.*, p.*
from combos c
inner join product_combos pc on pc.combo_id = c.combo_id
inner join products p on p.product_id = pc.product_id
where c.combo_id = ?
如果您真的想要,您甚至可以为每个组合重建产品的 csv 列表:
select c.combo_id, c.combo_name, group_concat(p.product_name) product_names
from products p
inner join product_combos pc on pc.product_id = p.product_id
inner jon combos c on c.combo_id = pc.combo_id
group by c.combo_id, c.combo_name