[英]PHP MySQL Multiple SQL Queries or JOIN for each query
If I have two tables with their respective columns: 如果我有两个带有各自列的表:
=====================
product_categories
=====================
id | name
=====================
=======================
products
=======================
id | category_id | name
=======================
And I have the following PHP functions: 而且我有以下PHP函数:
// Returns an array of all rows from the products
function getProducts() { ... }
// returns the corresponding row to the given id of the product category
function getProductCategory($category_id) { ... }
Currently, I am displaying a list of all products in a table with the following headings: 目前,我在带有以下标题的表格中显示所有产品的列表:
Where category is the name of the category corresponding to the category_id
of the product. 其中category是与产品的category_id对应的
category_id
名称。
Currently I am using a foreach loop to iterate through the product and calling the getProductCategory(...)
function for each product: 目前,我正在使用foreach循环来迭代产品,并为每个产品调用
getProductCategory(...)
函数:
<?php
...
$products = getProducts();
foreach ($products as $product) {
$product_category = getProductCategory($product['category_id']);
...
}
...
?>
Which would be an extensive amount of queries to the database if there are lots of products with many repeated queries. 如果有很多产品具有很多重复的查询,那将是对数据库的大量查询。
Would it be good practice if the getProducts()
function include all it's category information using a JOIN
in the SQ
L statement? 如果
getProducts()
函数在SQ
L语句中使用JOIN
包含所有类别信息,这将是一个好习惯吗?
If you want to get id
, category_name
and product_name
each time, you should make ONE method and avoid to make one select
, then a foreach
on each product and an other select
to get all value. 如果要每次获取
id
, category_name
和product_name
,则应使用ONE方法,并避免进行一次select
,然后对每种产品进行一次foreach
,而对另一项进行select
以获取所有价值。
So try something like this maybe : 所以尝试这样的事情也许:
function getProductsData() {
$querie = 'SELECT
p.id, p.name,
pc.name as Category
FROM products as p
LEFT JOIN product_categories as pc on pc.id = p.category_id
ORDER BY p.name; -- optionnal
';
// Get the data and return them
// You should get an array with one row =
// id (of the product) / name (of the product) / Category (the name of the category)
}
Yes, Join will be better. 是的,加入会更好。 It will reduce the number of queries for fetching the category.
它将减少获取类别的查询数量。 Otherwise, you can do the separate query for getting the categories as below code, but I will suggest Join.
否则,您可以单独查询以获取如下代码所示的类别,但我建议您加入。
$products = getProducts();
$categoryIds = [];
foreach ($products as $product) {
$categoryIds[] = $product['category_id'];
...
}
$product_category = getProductCategory($categoryIds);
You have to modify the query in getProductCategory()
and add where in
condition for comparing category ID In getProductCategory()
, you can implode the category IDs, for adding it into the where in
clause. 您必须在
getProductCategory()
修改查询,并where in
条件中添加where in
以便比较类别ID在getProductCategory()
,可以内嵌类别ID,以将其添加到where in
子句中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.