[英]Join three table in MySQL and get one row per id (one row per each entity in first table)
i have three table for products, product configs(eg iPhone x 64 black, iPhone x 64 red) and product inventories; 我有三个产品表,产品配置(例如,iPhone x 64黑色,iPhone x 64红色)和产品清单; I want to select all products and sort by price (in config table) and availability (in inventory table) but get only one row per product;
我想选择所有产品,并按价格(在配置表中)和可用性(在库存表中)排序,但每个产品仅获得一行; how can id do this?
id如何做到这一点? below is my mysql table structure
下面是我的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;
i tried this query : 我试过这个查询:
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
edit: with this query: 编辑:使用此查询:
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
i have below result; 我有以下结果; but i want one product per config with min price and max quantity
但是我想要每个配置一个产品的最小价格和最大数量
Try This: 尝试这个:
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
Try this one, see if it will work: 试试这个,看看是否行得通:
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;
Note that I still retain SELECT *
and added min(price),max(quantity)
at the end so you can do the comparison. 请注意,我仍然保留
SELECT *
并在最后添加了min(price),max(quantity)
,以便进行比较。 If this is not working for you, you may need to provide some data example and an illustration of how your desired output is. 如果这对您不起作用,则可能需要提供一些数据示例并说明所需输出的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.