【问题标题】:Join three table in MySQL and get one row per id (one row per each entity in first table)在 MySQL 中加入三个表并获得每个 id 一行(第一个表中每个实体一行)
【发布时间】:2019-06-08 11:52:30
【问题描述】:

我有三个产品表,产品配置(例如 iPhone x 64 黑色,iPhone x 64 红色)和产品库存;我想选择所有产品并按价格(在配置表中)和可用性(在库存表中)排序,但每个产品只有一行;我怎么能这样做? 下面是我的mysql表结构

CREATE TABLE `product_inventories` (
  `inventory_id` bigint(20) NOT NULL,
  `quantity` int(11) NOT NULL,
  `warehouse_id` bigint(20) DEFAULT NULL,
  `config_id` bigint(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `product_configs` (
  `config_id` bigint(20) NOT NULL,
  `product_id` bigint(20) DEFAULT NULL,
  `price` decimal(12,3) DEFAULT NULL,
  `discount_percent` int(11) DEFAULT NULL,
  `sku` varchar(30) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED;

CREATE TABLE `products` (
  `id` bigint(20) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `category_id` bigint(20) NOT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

我试过这个查询:

SELECT * 
FROM products 
    LEFT JOIN product_configs ON products.id = product_configs.product_id 
    LEFT JOIN product_inventories inventories ON inventories.inventory_id =
         (SELECT product_inventories.inventory_id from product_inventories
          WHERE product_inventories.config_id = product_configs.config_id
          ORDER by product_inventories.quantity DESC LIMIT 1 ) 
ORDER BY product_configs.price ASC, inventories.quantity DESC

编辑: 使用此查询:

SELECT *
FROM products 
    LEFT JOIN product_configs ON products.id = product_configs.product_id 
    LEFT JOIN product_inventories inventories ON inventories.inventory_id =
         (SELECT product_inventories.inventory_id from product_inventories
          WHERE product_inventories.config_idd = product_configs.config_id
          ORDER by product_inventories.quantity DESC LIMIT 1 ) 

ORDER BY product_configs.price is null, product_configs.price ASC, inventories.quantity DESC

我有以下结果;但我希望每个配置一个产品,最低价格和最大数量

【问题讨论】:

  • how can id do this?... 通过编写 SQL 查询。 Stack Overflow 不是免费的代码编写/家庭作业服务。如果您在这里需要帮助,您将不得不付出一些努力。
  • 请添加您的查询
  • @JoeTaras 查询已添加;
  • 在您的查询中有一个错误:“WHERE ORDER BY”您忘记了 where 子句吗? (虽然与问题无关)
  • @JulienB。已编辑;

标签: mysql sql join spring-data-jpa


【解决方案1】:

试试这个:

SELECT 
    ...,
    SUM(quantity) AS total_quantity
FROM products AS p
    LEFT JOIN product_configs AS pc ON pc.product_id = p.id
    LEFT JOIN product_inventories AS pi ON pi.config_id = pc.config_id
GROUP BY p.id
ORDER BY pc.price ASC, pi.quantity DESC

【讨论】:

  • 谢谢,但这是不正确的;因为我想要价格低廉且可用的配置(库存表中的数量大于零);但是即使配置数量为零,您的查询也会返回价格低廉的配置
【解决方案2】:

试试这个,看看是否可行:

SELECT *,MIN(price),MAX(quantity) FROM products LEFT JOIN product_configs ON products.id = product_configs.product_id LEFT JOIN product_inventories inventories ON inventories.inventory_id = (SELECT product_inventories.inventory_id FROM product_inventories INNER JOIN product_configs ON product_inventories.config_id = product_configs.config_id ORDER BY product_inventories.quantity DESC LIMIT 1 ) GROUP BY sku ORDER BY product_configs.price ASC, inventories.quantity DESC;

请注意,我仍然保留SELECT *,并在末尾添加min(price),max(quantity),以便您进行比较。如果这不适合您,您可能需要提供一些数据示例并说明您想要的输出是怎样的。

【讨论】:

  • 谢谢;帖子编辑并添加了一些额外信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多