简体   繁体   English

使用PHP从2个具有相同ID的表中进行MySQL JOIN

[英]MySQL JOIN from 2 tables with same ID using PHP

I got a table like: 我有一张桌子,像:

product 产品

id | product_id             
1     55                     
2     56                    
3     57                    

product_options 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 . 我想选择所有option_valuesproduct_options其中product_id产品一样的是product_idproduct_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. 问题是:如果我只有一个options_value ,那就很好。 but when I have 2 or more options_values the result is 0. 但是当我有2个或更多options_values ,结果为0。

i want to select all options_value from product_options for all product_id from product_options 我想选择所有options_valueproduct_options所有product_idproduct_options

PS. PS。 Sorry for my english and explication 对不起,我的英文和口译

使用具有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 在两个表之间使用内部联接,并使用product_id作为联接键

select p.id, p.product_id, po.optional_value
from products p inner join product_options po on p.product_id=po.product_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM