对3个表的嵌套mysql查询未显示正确的结果

jxct1oxe  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(322)

我对嵌套查询有问题。共有3个表gutschriften\u products,orders\u products,products
查询

CREATE TABLE IF NOT EXISTS `gutschriften_products` (
  `gut_orders_products_id` int(11) NOT NULL AUTO_INCREMENT,
  `gut_id` int(11) NOT NULL DEFAULT '0',
  `orders_products_id` int(11) NOT NULL DEFAULT '0',
  `orders_id` int(11) NOT NULL DEFAULT '0',
  `products_id` int(11) NOT NULL DEFAULT '0',
  `products_name` varchar(64) DEFAULT NULL,
  `products_price` decimal(15,4) NOT NULL DEFAULT '0.0000',
  `final_price` decimal(15,4) NOT NULL DEFAULT '0.0000',
  `products_quantity` int(2) NOT NULL DEFAULT '0',
  PRIMARY KEY (`gut_orders_products_id`)
) ;

INSERT INTO `gutschriften_products` (`gut_orders_products_id`, `gut_id`, `orders_products_id`, `orders_id`, `products_id`, `products_name`, `products_price`, `final_price`, `products_quantity`) VALUES
(1, 1, 12, 108, 375, 'Prod 375', '92.3529', '92.3529', 1),
(2, 2, 13, 109, 375, 'Prod 375', '65.9664', '65.9664', 1),
(3, 3, 51, 110, 377, 'Prod 377', '71.8487', '71.8487', 1),
(4, 4, 40, 111, 432, 'Prod 432', '82.7731', '82.7731', 1),
(5, 4, 41, 112, 427, 'Prod 427', '72.6891', '72.6891', 1),
(6, 4, 42, 113, 420, 'Prod 420', '72.6891', '72.6891', 1),
(7, 4, 43, 114, 423, 'Prod 423', '82.7731', '82.7731', 1),
(8, 4, 44, 115, 423, 'Prod 423', '82.7731', '82.7731', 1),
(9, 5, 59, 116, 451, 'Prod 451', '78.5714', '78.5714', 1);

====

CREATE TABLE IF NOT EXISTS `orders_products` (
  `orders_products_id` int(11) NOT NULL AUTO_INCREMENT,
  `orders_id` int(11) NOT NULL DEFAULT '0',
  `products_id` int(11) NOT NULL DEFAULT '0',
  `products_name` varchar(64) DEFAULT NULL,
  `products_price` decimal(15,4) NOT NULL DEFAULT '0.0000',
  `final_price` decimal(15,4) NOT NULL DEFAULT '0.0000',
  `products_quantity` int(2) NOT NULL DEFAULT '0',
  PRIMARY KEY (`orders_products_id`),
  KEY `orders_id` (`orders_id`),
  KEY `orders_id_2` (`orders_id`),
  KEY `products_id` (`products_id`)
);

INSERT INTO `orders_products` (`orders_products_id`, `orders_id`, `products_id`, `products_name`, `products_price`, `final_price`, `products_quantity`) VALUES
(12, 108, 375, 'Prod 375', '100.7563', '100.7563', 1),
(13, 109, 375, 'Prod 375', '78.5714', '78.5714', 1),
(14, 110, 376, 'Prod 376', '70.1681', '70.1681', 1),
(15, 111, 377, 'Prod 377', '63.0252', '63.0252', 1),
(16, 112, 376, 'Prod 376', '70.1681', '70.1681', 1),
(17, 113, 377, 'Prod 377', '92.3529', '92.3529', 1),
(18, 114, 375, 'Prod 375', '69.3277', '69.3277', 1),
(19, 115, 376, 'Prod 376', '117.5600', '117.5600', 1),
(20, 116, 377, 'Prod 377', '79.8319', '79.8319', 1);

====

CREATE TABLE IF NOT EXISTS `products` (
  `products_id` int(11) NOT NULL AUTO_INCREMENT,
  `products_quantity` int(4) NOT NULL DEFAULT '0',
  `products_price` decimal(15,8) NOT NULL DEFAULT '0.00000000',
  `products_date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `products_status` tinyint(1) NOT NULL DEFAULT '0',
  `manufacturers_id` int(11) DEFAULT NULL,
  `products_ordered` int(11) NOT NULL DEFAULT '0',
  `products_ek` double(15,8) NOT NULL DEFAULT '0.00000000',
  `products_verfuegbar` int(2) NOT NULL DEFAULT '1',
  `products_geprueft` datetime NOT NULL,
  `products_lagernd` int(4) NOT NULL,
  PRIMARY KEY (`products_id`),
  KEY `idx_products_date_added` (`products_date_added`)
) ;

INSERT INTO `products` (`products_id`, `products_quantity`, `products_price`, `products_date_added`, `products_status`, `manufacturers_id`, `products_ordered`, `products_ek`, `products_verfuegbar`, `products_geprueft`, `products_lagernd`) VALUES
(375, 18, '71.00840336', '2015-05-04 17:44:45', 1, 16, 75, 41.00000000, 1, '0000-00-00 00:00:00', 0),
(376, 10, '77.73109244', '2015-05-05 10:28:31', 1, 16, 67, 45.00000000, 1, '0000-00-00 00:00:00', 0),
(377, 10, '71.00840336', '2015-05-05 10:45:08', 1, 16, 52, 41.00000000, 1, '0000-00-00 00:00:00', 0),
(378, 8, '71.00840336', '2015-05-05 10:52:20', 1, 16, 35, 41.00000000, 1, '0000-00-00 00:00:00', 0),
(379, 10, '77.73109244', '2015-05-05 10:56:47', 1, 16, 36, 45.00000000, 1, '0000-00-00 00:00:00', 0);

我想知道哪些商品多久被卖出/退货一次。不幸的是,我的方法只打印那些已经售出和退回的商品。从未售出或退货的商品将不显示。

SELECT
(verkauft - gutschrift)*(P.products_price - products_ek) / (DATEDIFF(CURDATE(), P.products_date_added))*365 AS GEZE,
   DATEDIFF(CURDATE(), P.products_date_added) AS dtage,
   products_quantity,
   DATE_FORMAT(P.products_date_added, " % d. % m. % Y") AS hinzu,
   P.products_date_added,
   A.products_id,
   A.products_name,
   verkauft,
   gutschrift,
   final_price,
   verkauft - gutschrift AS summe,
   gutschrift / verkauft*100 AS rquote,
   products_ek AS EK,
   P.products_price AS VK,
   P.products_date_added AS aufgenommen,
   P.manufacturers_id,
   (
      verkauft - gutschrift
   )
   *(P.products_price - products_ek) AS gewinn,
   products_lagernd 
FROM
   (
      select
         products_id,
         products_name,
         sum(products_quantity) as verkauft 
      from
         orders_products 
      where
         orders_id BETWEEN 1 AND 11847 
      group by
         products_id
   )
   AS A 
   JOIN
      (
         select
            final_price,
            products_id,
            products_name,
            sum(products_quantity) as gutschrift 
         from
            gutschriften_products 
         where
            final_price > 0 
            AND orders_id BETWEEN 1 AND 11847 
         group by
            products_id
      )
      AS B 
      ON A.products_id = B.products_id 
   JOIN
      (
         select
            products_geprueft,
            products_id,
            products_status,
            manufacturers_id,
            products_quantity,
            products_ek,
            products_price,
            products_lagernd,
            products_date_added 
         from
            products
      )
      AS P 
      ON P.products_id = B.products_id 
where
   products_status > 0 
   AND 1 = 1 
order by
   products_name ASC,
   products_id

结果:只有两行产品标识号分别为375和377
但我还有另外3种产品376、378、379为什么没有列出来?

n7taea2i

n7taea2i1#

我知道我不该这么做,但我有点吃不消,所以(请将其与原始sql进行比较并得出结论)

SELECT
  (verkauft - gutschrift)*(P.products_price - products_ek) / (DATEDIFF(CURDATE(), P.products_date_added))*365 AS GEZE,
   DATEDIFF(CURDATE(), P.products_date_added) AS dtage,
   P.products_quantity,
   DATE_FORMAT(P.products_date_added, "%d.%m.%Y") AS hinzu,
   P.products_date_added,
   P.products_id,
   A.products_name,
   verkauft,
   gutschrift,
   final_price,
   verkauft - gutschrift AS summe,
   gutschrift / verkauft*100 AS rquote,
   products_ek AS EK,
   P.products_price AS VK,
   P.products_date_added AS aufgenommen,
   P.manufacturers_id,
   (verkauft - gutschrift ) *(P.products_price - products_ek) AS gewinn, 
   products_lagernd 
FROM products P
LEFT JOIN ( select
             products_id,
             products_name,
             sum(products_quantity) as verkauft 
            from orders_products 
            where orders_id BETWEEN 1 AND 11847 
            group by products_id ) AS A ON P.products_id = A.products_id
LEFT JOIN (select
              final_price,
              products_id,
              products_name,
              sum(products_quantity) as gutschrift 
           from gutschriften_products 
           where final_price > 0 AND orders_id BETWEEN 1 AND 11847 
           group by products_id ) AS B ON A.products_id = B.products_id    
where products_status > 0    
order by products_name ASC, products_id;

如果对你有好处,请回复。。。

相关问题