[英]How to fetch all the products from one table and merge one row from another table which contain multiple rows?
I have to tables. 我要桌子。 products
table and product_variants
table. products
表和product_variants
表。 In product table
, I have 在product table
,我有
id, product_name, photo
. id, product_name, photo
。
In product_variants
table, I have 在product_variants
表中,我有
id, product_id, packet_size, price
. id, product_id, packet_size, price
。
I have to add products first, then will add variants. 我必须先添加产品,然后再添加变体。 It is possible that no variants have been added for that. 可能没有为此添加任何变体。
I want to fetch all the product with one variant details from variants table with the lowest price. 我想以最低的价格从变量表中获取所有带有一个变量详细信息的产品。
Example scenario: 示例方案:
products table 产品表
id product_name photo
1 product_1 1.png
2 product_2 2.png
3 product_3 3.png
4 product_4 4.png
product_variants product_variants
id product_id packet_size price
1 1 100 ML 50 RS.
2 1 200 ML 100 Rs.
3 1 300 ML 150 RS.
4 2 300 L 300 Rs.
5 2 200 L 200 Rs.
6 3 200 K 200 Rs.
I want the result to be as below: 我希望结果如下:
1 product_1 1.png 100 ML 50 RS.
2 product_2 2.png 200 L 200 Rs.
3 product_3 3.png 200 K 200 Rs.
Thanks. 谢谢。
Based on all the other questions your can find for "mysql join group min" or "mysql join group max" there are some catches when using GROUP BY and MIN/MAX and JOINs. 基于“ mysql join group min”或“ mysql join group max”可以找到的所有其他问题,使用GROUP BY和MIN / MAX和JOIN时有一些问题。 You can use the following approach: 您可以使用以下方法:
For step 1 the query will look like this: 对于步骤1,查询将如下所示:
SELECT
product_id,
MIN(price) as price
FROM
product_variants
GROUP BY
product_id
This will result in the following output (ids may differ): 这将导致以下输出(标识可能不同):
+------------+-------+
| product_id | price |
+------------+-------+
| 1 | 50 |
| 2 | 200 |
| 4 | 200 |
+------------+-------+
For step 2 we use this result in a JOIN query on itself: 对于步骤2,我们在自身的JOIN查询中使用此结果:
SELECT
v2.product_id,
v2.packet_size,
v2.price
FROM
product_variants v2
JOIN
(SELECT -- here is your first query
product_id,
MIN(price) as price
FROM
product_variants
GROUP BY
product_id
) v1 -- notice the "v1" alias here, its needed
USING (product_id, price)
The result will be: 结果将是:
+------------+-------------+-------+
| product_id | packet_size | price |
+------------+-------------+-------+
| 1 | 100 ML | 50 |
| 2 | 200 L | 200 |
| 4 | 200 L | 200 |
+------------+-------------+-------+
And for step 3 it's just a "normal" JOIN between two tables: 对于步骤3,这只是两个表之间的“普通”联接:
SELECT
p.id,
p.product_name,
p.photo,
v3.packet_size,
v3.price
FROM
products p
JOIN
(SELECT -- here is query 2
v2.product_id,
v2.packet_size,
v2.price
FROM
product_variants v2
JOIN
(SELECT -- here is query 1
product_id,
MIN(price) as price
FROM
product_variants
GROUP BY product_id
) v1
USING (product_id, price)
) v3
ON
p.id = v3.product_id;
And this will get you the following result (again ids may differ): 这将为您带来以下结果(ID可能再次不同):
+----+--------------+-------+-------------+-------+
| id | product_name | photo | packet_size | price |
+----+--------------+-------+-------------+-------+
| 1 | product_1 | 1.png | 100 ML | 50 |
| 2 | product_2 | 2.png | 200 L | 200 |
| 4 | product_3 | 3.png | 200 L | 200 |
+----+--------------+-------+-------------+-------+
And if you use a LEFT JOIN instead of a "normal" JOIN, you get products as well which doesn't have any variants: 而且,如果您使用LEFT JOIN而不是“普通” JOIN,则还将获得没有任何变体的产品:
+----+--------------+-------+-------------+-------+
| id | product_name | photo | packet_size | price |
+----+--------------+-------+-------------+-------+
| 1 | product_1 | 1.png | 100 ML | 50 |
| 2 | product_2 | 2.png | 200 L | 200 |
| 4 | product_3 | 3.png | 200 L | 200 |
| 5 | product_4 | 4.png | NULL | NULL |
+----+--------------+-------+-------------+-------+
The big catch is to fetch the remaining info of a found variant. 最大的困难是获取找到的变体的剩余信息。 You cannot do this in one query because the group function will fetch the correct value, but the other column values after the GROUP BY will not be from the found group function value, but instead will be most likely from the first row in the group. 您无法在一个查询中执行此操作,因为组函数将获取正确的值,但是GROUP BY之后的其他列值将不是来自找到的组函数值,而是很有可能来自组的第一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.