使用PHP从两个具有相同ID的表进行MySQL连接
我有一张这样的桌子:使用PHP从两个具有相同ID的表进行MySQL连接,php,mysql,Php,Mysql,我有一张这样的桌子: id | product_id 1 55 2 56 3 57 产品 id | product_id 1 55 2 56 3 57
id | product_id
1 55
2 56
3 57
产品
id | product_id
1 55
2 56
3 57
产品选项
id | product_id
1 55
2 56
3 57
id | product_id | options_value
1 55 88
2 55 87
3 55 89
id | product_id
1 55
2 56
3 57
我想从产品选项中选择所有选项
值,其中产品id
来自产品与产品id
来自产品相同
id | product_id
1 55
2 56
3 57
从产品中选择所有字段后,我将使用以下内容:
id | product_id
1 55
2 56
3 57
$sql .= " LEFT JOIN " . DB_PREFIX . "product_option_value ovi ON (ovi.product_id=p.product_id)";
if(...){
$sql .= " AND ovi.option_value_id='$value'";
}
问题是:如果我只有一个选项\u值
,就可以了。
但是当我有2个或更多的选项\u值时,结果是0
id | product_id
1 55
2 56
3 57
我想从产品选项中为所有产品id
从产品选项中选择所有选项\u值
id | product_id
1 55
2 56
3 57
id | product_id | options_value
1 55 88
2 55 87
3 55 89
请原谅我的英语和解释使用product_id作为连接键在两个表之间使用内部连接
id | product_id
1 55
2 56
3 57
select p.id, p.product_id, po.optional_value
from products p inner join product_options po on p.product_id=po.product_id
使用product_id作为联接键在两个表之间使用内部联接
id | product_id
1 55
2 56
3 57
select p.id, p.product_id, po.optional_value
from products p inner join product_options po on p.product_id=po.product_id
使用具有产品标识的右连接
id | product_id
1 55
2 56
3 57
select p.id, p.product_id, po.options_value from products p right join product_options po on p.product_id=po.product_id
使用具有产品标识的右连接
id | product_id
1 55
2 56
3 57
select p.id, p.product_id, po.options_value from products p right join product_options po on p.product_id=po.product_id
请看:请向我们展示整个查询并提供您所需的输出示例。请向我们展示整个查询并提供您所需的输出示例。您可以提供您的预期输出吗?您可以提供您的预期输出吗