我有3个数据库表,我需要信息来自。我需要的信息是缩进的。
--products
--product_image
--order_options
--option_value
--order_products
--product_id
现在我有以下代码
foreach ($db->query("SELECT * FROM products WHERE product_id= $product_id") as $row)
order_product[] = $row;
foreach ($db->query("SELECT * FROM order_options WHERE order_id= $order_id") as $row)
order_size[] = $row;
foreach ($db->query("SELECT * FROM order_products WHERE order_id= $order_id") as $row)
order_source[] = $row;
(order_id
来自脚本中的$order_id = $_GET['id'];
。)
我想将所有三个select语句放在一个查询中。我已经看到了这一点,但从来没有真正理解我找到的任何东西(即an old question from this site)我理解youtube video我看过并设法将上述内容更改为以下内容:
foreach ($db->query("SELECT * FROM [order_products] as opro INNER JOIN order_options as oopt ON opro.order_id = oopt.order_id") as $row)
$order_variables[] = $row;
虽然我运行时遇到此错误:
Warning: Invalid argument supplied for foreach() in /home/www/libraries/phppwinty/print.php on line 35
那么如何在这些JOIN
表之间取得成功3
?
答案 0 :(得分:0)
这样的事情应该有效。想法:按照product_id加入产品,order_id加入order_options,并在指定的ID上加上order_products:
SELECT orderproducts.product_id, orderoptions.option_value, products.product_image
FROM order_products as orderproducts
INNER JOIN order_options as orderoptions ON orderproducts.order_id = orderoptions.order_id
INNER JOIN products as products ON orderproducts.product_id = products.product_id
WHERE
orderproducts.order_id = $order_id AND
orderproducts.product_id = $product_id