我试图使用sql划分两个选择查询,不确定这是否可行

时间:2016-04-19 12:48:42

标签: mysql sql

以下是我的查询,我认为原则上它应该有效,但我不确定它是否确实可行,或者我在想这个问题的方法是否过时。

SELECT 
  (SELECT `orders`.`Status`, COUNT(*) AS COUNT_2 FROM `orders` `sw_orders` WHERE STATUS = 'booking' AND Date(OrderDate) <= CURDATE() AND Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY)) / 
  (SELECT `orders`.`Status`, COUNT(*) AS COUNT_2 FROM `orders` `sw_orders` WHERE STATUS = 'quote' AND Date(OrderDate) <= CURDATE() AND Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY))
AS result

这应该返回2个结果的值,其中预订除以引号

3 个答案:

答案 0 :(得分:1)

SELECT count(case when STATUS = 'booking' then 1 end) /
           count(case when STATUS = 'quote' then 1 end)
FROM `sw_orders`
WHERE Date(OrderDate) <= CURDATE()
  AND Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY) 

答案 1 :(得分:0)

select count(status ='booking' or null) / count(status = 'quote') as result from table_name where  Date(OrderDate) <= CURDATE() AND Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY)

请注意语法错误。我没有照顾。

答案 2 :(得分:0)

SELECT 
   SUM(CASE WHEN STATUS = 'booking' THEN 1 ELSE 0 END) /
   SUM(CASE WHEN STATUS = 'quote' THEN 1 ELSE 0 END)
FROM `sw_orders` 
WHERE Date(OrderDate) <= CURDATE() 
  AND Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY)

我想COUNT的状态=&#39;引用&#39;不是0