2017-04-26 42 views
0

我試圖計算「票」列中的兩個值「1」和「2」的出現次數,但當我添加另一個時出現錯誤從「where」和「from」開始。兩個計數請求與雙從哪裏和從一個查詢

「門票」是一列,「門票」是一張表。

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

除了子查詢,這些不是,查詢可能只有一個FROM和一個WHERE。 – Uueerdo

回答

0

使用條件彙總:

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

您正在尋找有條件的聚集,像這樣:

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

COUNT只能算作非空值。

1
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 

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

嘗試這樣

0

你的查詢語法無效,試試這個:

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

嘗試了這一點,讓我知道在任何情況下,查詢。

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