【发布时间】:2018-08-31 10:43:35
【问题描述】:
我正在建立一个小型在线商店,其中有一个名为“products”的父表和一个名为“images”的子表。
我遵循了最佳实践并设置了一个外键约束,其中字段product_id 建立了两个表之间的链接。
产品
product_id (PK parent table)
product_title
product_category_id
product-price
product_quantity
product_description
long_description
product_image
图片
image_id (PK for child table)
product_id (foreign key)
product_image_two
注意:每个产品将有 2 张图片,因此我想根据其 product_id 检索产品并从每个表中获取关联的图片。
即。查询从“产品”中提取product_image,从“图像”中提取product_image_two
我在这里浏览了大量关于 JOIN 的帖子,并试图重构其他人的代码,但到目前为止没有成功。
我目前的陈述
<?php
$query = query("SELECT p.* , i.* FROM products p,images i WHERE p.product_id=i.product_id");
confirm($query);
while ($row = fetch_array($query)):
?>
【问题讨论】:
-
您能提供一些样本数据并期待结果吗?你能详细解释一下
without success. -
您的问题究竟是什么?您是否收到错误、意外结果?
-
是的,在我运行查询时澄清一下,它似乎只提取 product_image 而不是 product_image_two