Mysql 获得库存产品数量

Mysql 获得库存产品数量,mysql,join,aggregate-functions,Mysql,Join,Aggregate Functions,我有非常困难的查询,我需要获得参数parameterValue_id和number products,其中包含库存上的此类参数。我只想计算符合其他标准的产品 SELECT `parameters`.`parameter_from`, `parametervalues`.*, (SELECT IF (sumOfProductIN - sumOfProductOUT > 0, COUNT(productparametervalues.f_product_id), 0) FROM product

我有非常困难的查询,我需要获得参数parameterValue_id和number products,其中包含库存上的此类参数。我只想计算符合其他标准的产品

SELECT `parameters`.`parameter_from`, `parametervalues`.*, (SELECT IF (sumOfProductIN - sumOfProductOUT > 0, COUNT(productparametervalues.f_product_id), 0) FROM productparametervalues`
LEFT JOIN `products` ON products.product_id = productparametervalues.f_product_id
LEFT JOIN `categories` ON categories.category_id = products.f_category_id
LEFT JOIN (SELECT oi1.f_product_id, SUM(oi1.orderItem_numberUnits) AS sumOfProductIN
FROM orderitems oi1 WHERE oi1.orderItem_type = 'IN' GROUP BY oi1.f_product_id) AS `t` ON t.f_product_id = products.product_id
LEFT JOIN (SELECT oi2.f_product_id, SUM(oi2.orderItem_numberUnits) AS sumOfProductOUT
FROM orderitems oi2 WHERE oi2.orderItem_type = 'OUT' GROUP BY oi2.f_product_id) AS `t_2` ON t_2.f_product_id = products.product_id
LEFT JOIN `parametervalues` ON parametervalues.f_parameter_id = parameters.parameter_id
WHERE (category_lft >= '8') AND (category_rgt <= '19') AND (products.product_price >= '1') AND (products.product_price <= '2500') AND (products.product_enabled = 1)
WHERE (parameter_id = '4') AND (parameters.parameter_enabled = 1) AND (parametervalues.parameterValue_enabled = 1)
GROUP BY `parametervalues`.`parameterValue_id`) AS countProducts FROM `parameters`
表orderitems 表参数 表参数值 表productparametervalues 餐桌用品
你能共享模式吗?@Arun Kumar M I添加了查询中的表的结构
CREATE TABLE IF NOT EXISTS `categories` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_title` varchar(255) DEFAULT NULL,
`category_description` text,
`f_parent_id` int(11) DEFAULT NULL,
`category_lft` smallint(6) DEFAULT NULL,
`category_rgt` smallint(6) DEFAULT NULL,
`category_depth` smallint(6) DEFAULT NULL,
`category_enabled` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`category_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
CREATE TABLE IF NOT EXISTS `orderitems` (
`orderItem_id` int(11) NOT NULL AUTO_INCREMENT,
`f_order_id` int(11) DEFAULT NULL,
`f_product_id` int(11) DEFAULT NULL,
`orderItem_title` varchar(50) DEFAULT NULL,
`orderItem_numberUnits` smallint(6) NOT NULL DEFAULT '1',
`orderItem_pricePerUnit` decimal(18,5) NOT NULL DEFAULT '0.00000',
`orderItem_price` float(18,5) NOT NULL DEFAULT '0.00000',
`orderItem_type` varchar(10) DEFAULT NULL,
PRIMARY KEY (`orderItem_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=95 ;
CREATE TABLE IF NOT EXISTS `parameters` (
`parameter_id` int(11) NOT NULL AUTO_INCREMENT,
`f_parameterGroup_id` int(11) DEFAULT NULL,
`parameter_title` varchar(100) DEFAULT NULL,
`parameter_enabled` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`parameter_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
CREATE TABLE IF NOT EXISTS `parametervalues` (
`parameterValue_id` int(11) NOT NULL AUTO_INCREMENT,
`f_parameter_id` int(11) NOT NULL,
`parameterValue_title` varchar(100) NOT NULL,
`parameterValue_enabled` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`parameterValue_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ;
CREATE TABLE IF NOT EXISTS `productparametervalues` (
`f_product_id` int(11) NOT NULL AUTO_INCREMENT,
`f_parameter_id` int(11) NOT NULL DEFAULT '0',
`f_parameterValue_id` int(11) NOT NULL DEFAULT '0',
`productParameter_value` varchar(100) DEFAULT NULL,
PRIMARY KEY (`f_product_id`,`f_parameter_id`,`f_parameterValue_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=41 ;
CREATE TABLE IF NOT EXISTS `products` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`f_category_id` int(11) DEFAULT NULL,
`f_brand_id` int(11) DEFAULT NULL,
`product_title` varchar(100) DEFAULT NULL,
`product_description` text,
`product_price` float(10,2) DEFAULT NULL,
`product_enabled` tinyint(1) DEFAULT '0',
PRIMARY KEY (`product_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=43 ;