简体   繁体   English

如何从一个表中获取所有产品,并从包含多行的另一张表中合并一行?

[英]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: 您可以使用以下方法:

  1. SELECT (only) the min price of a variant for a given product. 选择(仅)给定产品的变体的最低价格。
  2. SELECT the remaining info of the found variant. 选择找到的变体的剩余信息。
  3. SELECT the product info of the found variant. 选择找到的变体的产品信息。

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.

相关问题 如何将一个表中的行合并到另一表中的另一行 - How to merge rows from one table to another row in another table 如何从表中删除一行,而在另一表上删除多行相关? - How to delete one row from a table and multiple rows on another that are related? 如何从一个表中选择单行和从另一个表中选择多行? - how to select single row from one table and multiple rows from another table toghether? 使用单个查询从一个表中获取行,并引用包含另一表中数据列表的行 - Fetch rows from one table with reference to a row with list of data from another table using single query 从一个表中选择所有行,并为另一表中的每一行选择一个特定值 - Select all rows from one table and one specific value for each row from another table 如何获取与另一个表中的多行相关的行? - How to fetch a row which is related to multiple rows in another table? 将表格中的一行与多行联接 - Join one row from a table with multiple rows 一个父行,另一个表中的多个子行。 如何将它们全部排成一排? - One parent row, multiple child rows in another table. How to get them all in one row? 将一个表中的所有行放到另一个表中 - Put all rows from one table to another 将链接数据从一个表中的一行提取到另一表中的多行 - Extract linked data from one row in one table to multiple rows in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM