简体   繁体   English

PHP MySQL多个SQL查询或每个查询的JOIN

[英]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: 目前,我在带有以下标题的表格中显示所有产品的列表:

  • id ID
  • Category 类别
  • Name 名称

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. 如果要每次获取idcategory_nameproduct_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.

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