按条件汇总数据库值

时间:2018-02-08 13:41:16

标签: mysql

我通过此查询获得总运费

SELECT SUM(ot.value/o.currency_value) AS shipping
FROM   orders o
JOIN   orders_total ot ON (ot.orders_id = o.orders_id AND ot.class = 'ot_shipping')
WHERE  o.date_purchased >= '2018-01-01 00:00:00'
AND    o.date_purchased < '2018-01-02 00:00:00'
AND    o.orders_status != 8

一个order_id的数据库条目如下所示

INSERT INTO `orders_total` (`orders_total_id`, `orders_id`, `title`, `text`, `value`, `class`, `sort_order`) VALUES
(17057, 3265, 'Zwischensumme:', '49,04 EUR', '49.0352', 'ot_subtotal', 10),
(17058, 3265, 'Pauschale Versandkosten (Bester Weg):', '4,96 EUR', '4.9600', 'ot_shipping', 25),
(17059, 3265, '<b>Summe, netto</b>:', '<b>54,00 EUR</b>', '53.9952', 'ot_subtotal_no_tax', 40),
(17060, 3265, 'zzgl. MwSt. 19%:', '10,26 EUR', '10.2591', 'ot_tax', 50),
(17061, 3265, '<b>Summe, brutto</b>:', '<b>64,26 EUR</b>', '64.2576', 'ot_total', 99);

我想将运费分成netto和gross值。 该计划可能是

SELECT IF(ot_title = 'zzgl. MwSt. 19%:', 
           SUM(ot.value/o.currency_value),  
           SUM((ot.value/1.19)/o.currency_value)
         ) AS shipping
FROM   orders o
JOIN   orders_total ot 
  ON (ot.orders_id = o.orders_id AND ot.class = 'ot_shipping')
WHERE  o.date_purchased >= '2018-01-01 00:00:00'
AND    o.date_purchased < '2018-01-02 00:00:00'
AND    o.orders_status != 8

但IF条件不起作用因为我需要访问class ='ot_tax'的数据库行。

有没有办法用一个mysql查询实现它?

2 个答案:

答案 0 :(得分:0)

编辑:OP提供更多信息后更新,包括模式

您只需要将orders_total表左键加入自己,以在每行中包含所需的税字段。然后在IF条件中使用字段值:

SELECT SUM(
         IF(ot2.title = 'inkl. MwSt. 19%:',
            ot.value / 1.19,
            ot.value
         )
       ) as shipping
  FROM orders_total ot
  LEFT JOIN orders_total ot2 
    ON (ot.orders_id = ot2.orders_id AND ot2.class = 'ot_tax')
  WHERE ot.class = 'ot_shipping';

输出:

shipping
9.91798319

为了更好地理解行结构,请查看这个更简单的查询版本而不进行聚合:

SELECT ot.orders_id,  ot.value as shipping, ot2.title as tax 
  FROM orders_total ot
  LEFT JOIN orders_total ot2 
    ON (ot.orders_id = ot2.orders_id AND ot2.class = 'ot_tax')
  WHERE ot.class = 'ot_shipping';

输出:

orders_id   shipping     tax
3364             5.9     inkl. MwSt. 19%:
3375            4.96     zzgl. MwSt. 19%:

答案 1 :(得分:0)

现在有更多细节。这是数据表架构

CREATE TABLE IF NOT EXISTS `orders_total` (
  `orders_total_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `orders_id` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `text` varchar(255) NOT NULL,
  `value` decimal(15,4) NOT NULL,
  `class` varchar(32) NOT NULL,
  `sort_order` int(11) NOT NULL,
  PRIMARY KEY (`orders_total_id`),
  KEY `idx_orders_id` (`orders_id`),
  KEY `class` (`class`)
);

这些是条目:

INSERT INTO `orders_total` (`orders_id`, `title`, `text`, `value`, `class`, `sort_order`) VALUES
(3364, 'Zwischensumme:', '35,38 EUR', '35.3800', 'ot_subtotal', 10),
(3364, 'Pauschale Versandkosten (Bester Weg):', '5,90 EUR', '5.9000', 'ot_shipping', 25),
(3364, 'inkl. MwSt. 19%:', '6,59 EUR', '6.5900', 'ot_tax', 50),
(3364, '<b>Summe</b>:', '<b>41,28 EUR</b>', '41.2800', 'ot_total', 99),
(3375, 'Zwischensumme:', '112,49 EUR', '112.4874', 'ot_subtotal', 10),
(3375, 'Pauschale Versandkosten (Bester Weg):', '4,96 EUR', '4.9600', 'ot_shipping', 25),
(3375, '<b>Summe, netto</b>:', '<b>117,45 EUR</b>', '117.4474', 'ot_subtotal_no_tax', 40),
(3375, 'zzgl. MwSt. 19%:', '22,32 EUR', '22.3150', 'ot_tax', 50),
(3375, '<b>Summe, brutto</b>:', '<b>139,76 EUR</b>', '139.7598', 'ot_total', 99);

正如您所看到的,orders_id 3364包含税,这就是运费为5,90欧元的原因。 orders_id 3375不含税,运费为4,96欧元(4,96 + 19%增值税= 5,90欧元)。

使用我的sql_query,运费总和应为9,92欧元(均为4,96欧元)。