简体   繁体   English

SQL 我如何根据在数组中找到的值使用准备好的语句连接两个表

[英]SQL how do I join two tables using prepared statements based on values found in an array

I have the table products which contains the following:我有包含以下内容的表格产品:

product_id | name | category | img
1            car     toy        tcar.jpg
2            boat    toy        tboat.jpg
3            plane   actual     plane.jpg

I also have the table product which contains the following:我还有包含以下内容的表格产品:

product_id | size | price 
1            large   5
2            small   3
2            medium  4
3            small   7

Finally, I have a multidimensional array called $products_in_cart, with key equal to product_id.最后,我有一个名为 $products_in_cart 的多维数组,其键等于 product_id。 It stores the following values:它存储以下值:

Array ( [1] => Array ( [Quantity] => 1 [Size] => large) [2] => Array ( [Quantity] => 5 [Size] => medium ) )

I want to inner join products and product and then fetch the products which are found in the array $products_in_cart.我想内部加入产品和产品,然后获取在数组 $products_in_cart 中找到的产品。 (so I will fetch based on the product_id (the keys in $products_in_cart) as well as the size). (所以我将根据 product_id($products_in_cart 中的键)以及大小来获取)。 This has to be done using prepared statements.这必须使用准备好的语句来完成。

I managed to fetch products which are found in $products_in_cart using these statements:我设法使用这些语句获取在 $products_in_cart 中找到的产品:

$array_to_question_marks = implode(',', array_fill(0, count($products_in_cart), '?'));
$stmt = $pdo->prepare('SELECT * FROM products WHERE product_id IN (' . $array_to_question_marks . ')');
$stmt->execute(array_keys($products_in_cart));
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

This gives the output:这给出了 output:

Array ( [0] => Array ( [product_id] => 1 [name] => car [category] => toy [img] => tcar.jpg) [1] => Array ( [product_id] => 2 [name] => boat [category] => toy [img] => tboat.jpg))

However, what I want is to get this output:然而,我想要的是得到这个 output:

Array ( [0] => Array ( [product_id] => 1 [name] => car [category] => toy [img] => tcar.jpg [size] => large [price] => 5) [1] => Array ( [product_id] => 2 [name] => boat [category] => toy [img] => tboat.jpg [size] => medium [price] => 4))

I expect that I should inner join products and product and then pick those with id's and size that match the ones in $products_in_cart.我希望我应该在内部加入产品和产品,然后选择那些 ID 和尺寸与 $products_in_cart 中的相匹配的产品。 However I got no clue on how to do that (prepared statements are making the process complicated for me).但是我不知道如何做到这一点(准备好的陈述使我的过程变得复杂)。

I'm sorry for my poor description of the problem, but I hope that the example of what I managed to do makes it clear , thanks!对于我对问题的糟糕描述,我深表歉意,但我希望我设法做的事情的例子能说明问题,谢谢!

Well, if you want to do a inner join, just... do an inner join.好吧,如果您想进行内部联接,只需...进行内部联接。 Nothing says you can't do one within a prepared statement!没有人说你不能在准备好的声明中做一个!

Replace this line替换这一行

$stmt = $pdo->prepare('SELECT * FROM products WHERE product_id IN (' . $array_to_question_marks . ')');

With this one (I'm spliting it in lines for better clarity):有了这个(为了更清楚起见,我将它分成几行):

$stmt = $pdo->prepare('SELECT * FROM products 
            INNER JOIN product 
            ON (products.product_id = product.product_id) 
            WHERE products.product_id IN (' . $array_to_question_marks . ')');

You could also use the "shorthand" version if you prefer:如果您愿意,也可以使用“速记”版本:

$stmt = $pdo->prepare('SELECT * FROM products, product 
            WHERE products.product_id = product.product_id 
            AND products.product_id IN (' . $array_to_question_marks . ')');

As a side note, using two similar named tables is confusing.作为旁注,使用两个相似的命名表会造成混淆。 Since the "product" table contains sizes and prices and no product information, you could rename it to something like "prices_sizes" or something like that.由于“产品”表包含尺码和价格但没有产品信息,您可以将其重命名为“prices_sizes”之类的名称。

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

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