Answer the question
In order to leave comments, you need to log in
How to join tables in MySQL?
Good evening.
There are two tables:
The products table, which contains the name and description of the product.
Products_variants table, which contains trade offers: product_id, price, count.
One product can have several offers.
SELECT *
FROM products
LEFT JOIN products_variants ON products_variants.product_id = products.id
Answer the question
In order to leave comments, you need to log in
2 solutions:
1)
We do everything with one request (you can not change the current request), i.e. you will have many rows with the same product_id (because the product_variants table is tied to product, like 1 : M ).
then on the backend you do something like this:
$prevId = 0;
$resultArr = array();
foreach($result as $row){
if($prevId != $row['product_id'])
{
$resultArr[$row['product_id']] = $row; //сюда ложим поля таблицы product
$resultArr[$row['product_id']]['offers'] = $row['...']//ложим оффер первой строки, если нет- пустой массив
} else
{
$resultArr[$row['product_id']]['offers'] = $row['...']//ложим только необходимые поля
}
$prevId = $row['product_id'];
}
print_r($resultArr);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question