如果我有两个表,分别有各自的列:
=====================
product_categories
=====================
id | name
=====================
=======================
products
=======================
id | category_id | name
=======================
我有以下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) { ... }
目前,我在一个表格中显示所有产品的列表,标题如下:
身份证件
类别
名字
其中,category是与
category_id
产品的。
目前,我使用foreach循环迭代产品并调用
getProductCategory(...)
每个产品的功能:
...
$products = getProducts();
foreach ($products as $product) {
$product_category = getProductCategory($product['category_id']);
...
}
...
?>
如果有很多产品有许多重复的查询,这将是对数据库的大量查询。
如果
getProducts()
函数使用
JOIN
在
SQ
L声明?