Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP从两个具有相同ID的表进行MySQL连接_Php_Mysql - Fatal编程技术网

使用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

请看:请向我们展示整个查询并提供您所需的输出示例。请向我们展示整个查询并提供您所需的输出示例。您可以提供您的预期输出吗?您可以提供您的预期输出吗