MySQL使用PHP从2个具有相同ID的表中加入
I got a table like:
product
id | product_id
1 55
2 56
3 57
product_options
id | product_id | options_value
1 55 88
2 55 87
3 55 89
... ...
I want to select all option_values
from product_options where product_id
from product is same with product_id
from product_options.
After I select all fields from product I use this:
$sql .= " LEFT JOIN " . DB_PREFIX . "product_option_value ovi ON (ovi.product_id=p.product_id)";
if(...){
$sql .= " AND ovi.option_value_id='$value'";
}
The problem is: If I only got one options_value
, it's fine.
but when I have 2 or more options_values
the result is 0.
i want to select all options_value
from product_options for all product_id
from product_options
PS. Sorry for my english and explication
我得到的表如下: p>
产品 strong > p>
product_options strong> p>
... ... p>
我想要 从 product_options strong>中选择所有 从产品中选择所有字段后,我使用: p>
问题是:如果我只有一个 我想从 product_options strong>中为所有 PS。 对不起我的英文和解释 p>
div>
id | product_id
1 55
2 56
3 57
code> pre>
id | product_id | options_value
1 55 88
2 55 87
3 55 89
code> pre>
option_values code>,其中产品 strong>中的
product_id code>与
product_id code>相同 product_options strong>。 p>
$ sql。=“LEFT 加入“。 DB_PREFIX。 “product_option_value ovi ON(ovi.product_id = p.product_id)”;
if(...){
$ sql。=“AND ovi.option_value_id ='$ value'”;
}
代码> pre>
options_value code>,那很好。
但是当我有2个或更多
options_values code>时 结果是0。 p>
product_id code>从 options_value code> 强> product_options 强> p>
Use right join with product_id
select p.id, p.product_id, po.options_value from products p right join product_options po on p.product_id=po.product_id
Use inner join between two table using product_id as join key
select p.id, p.product_id, po.optional_value
from products p inner join product_options po on p.product_id=po.product_id