获得结果的更好方法

时间:2017-04-13 17:25:37

标签: mysql sql

想知道是否有更好的方式来运行此查询,因为BETWEEN重复自我,我只需要添加一个新规则(and local=1) OUTPUT就是我想要的,只是想知道是否有'更短或更好的版本。

SCHEMA&结果:http://sqlfiddle.com/#!9/e147c/2

SELECT COUNT(*) as total, 
(SELECT COUNT(*) FROM orden WHERE actual_date BETWEEN '2017-01-01' AND '2017-01-05' and local=1 and forma_pago='tarjeta' and status=4) as Principal 
FROM orden 
WHERE actual_date BETWEEN '2017-01-01' AND '2017-01-05'
and forma_pago='tarjeta' 
and status=4

1 个答案:

答案 0 :(得分:2)

这应该这样做:

SELECT COUNT(*) AS total
    , SUM(CASE
            WHEN local = 1 THEN 1
            ELSE 0
         END) AS Principal
FROM   orden
WHERE  actual_date BETWEEN '2017-01-01' AND '2017-01-05'
      AND forma_pago = 'tarjeta'
      AND status = 4;

sql小提琴:

http://sqlfiddle.com/#!9/e147c/3