两个计数请求在一个查询中使用double where和from

时间:2017-04-26 22:39:56

标签: mysql sql

我正在尝试计算“ticket”列中两个值“1”和“2”的出现次数,但是当我从“where”和“from”添加另一行时出现错误。< / p>

“ticket”是一列,“ticket”是一张表。

SELECT 
COUNT(ticket) as child FROM tickets WHERE ticket = 1,
COUNT(ticket) as adult FROM tickets WHERE ticket = 2
GROUP by ticket

5 个答案:

答案 0 :(得分:1)

import numpy as np
from numpy import pi as pi

# 'a' matrix:
a = np.array([[ 0.291761097764E+01 , 0.000000000000E+00 , 0.574463427333E+01  ],
              [ -0.145880548882E+01 , 0.252672522500E+01 , 0.574463427333E+01  ],
              [ -0.145880548882E+01 ,-0.252672522500E+01 , 0.574463427333E+01  ]])

# 'a' vectors:
a_1 = [ 0.291761097764E+01 , 0.000000000000E+00 , 0.574463427333E+01  ]
a_2 = [ -0.145880548882E+01 , 0.252672522500E+01 , 0.574463427333E+01  ]
a_3 = [ -0.145880548882E+01 ,-0.252672522500E+01 , 0.574463427333E+01  ]

# volume of 'a' matrix:
V_a = np.linalg.det(a)
print 'V_a = ', V_a

cte = (2*pi)/V_a

VEC_1 = np.cross(a_2, a_3)
VEC_2 = np.cross(a_3, a_1)
VEC_3 = np.cross(a_1, a_2)

# calculation of 'b' vectors:
b_1 = cte * VEC_1
b_2 = cte * VEC_2
b_3 = cte * VEC_3

print  'b_1 = ',  b_1
print  'b_2 = ',  b_2
print  'b_3 = ',  b_3

# building 'b' matrix:
b = np.vstack ([  b_1 , b_2,  b_3 ])
print 'b = ', b

# volume of 'b' matrix:
V_b = np.linalg.det(b)
print 'V_b = ', V_b
print "1/V_a = ", 1/V_a

print "1/V_a should be equal to V_b; however, we do not get this equality"

SELECT 
COUNT(if(ticket = 1,1,NULL)) as child,
COUNT(if(ticket = 2,1,NULL)) as adult FROM tickets where ticket in (1,2)
GROUP by ticket

试试这个

答案 1 :(得分:0)

使用条件聚合:

SELECT SUM(t.ticket = 1) as t1,
       SUM(t.ticket = 2) as t2
FROM tickets t;

答案 2 :(得分:0)

您正在寻找条件聚合,如下所示:

SELECT COUNT(IF(ticket=1, 1, NULL) AS child
   , COUNT(IF(ticket=2, 1, NULL)) AS adult
FROM tickets
;

COUNT仅计算非空值。

答案 3 :(得分:0)

您的查询在语法上无效,请尝试以下方法:

   Select count(*)
        , ticket
     from tickets
    where ticket In (1,2)
 Group by ticket
        ;

答案 4 :(得分:0)

尝试此操作,如有任何疑问,请与我联系。

SELECT SUM(ticket = 1) as t1,
       SUM(ticket = 2) as t2
FROM tickets
group by tickets;