我編寫了以下查詢以返回有關在X時間內進行的購買的一些統計信息。但由於某些原因,每個「COUNT」列都會返回總行數。我是否錯誤地組織了查詢?SQL查詢未返回預期結果
SELECT COUNT(*) as countTotal, SUM(`cost`) as cost, COUNT(`paymentType` = 'credit') as count_credit, COUNT(`paymentType` = 'cash') as count_cash
FROM `purchase` WHERE `date` >= '2011-5-4'
更新
我只是決定使用子查詢。這是我結束了。
SELECT
COUNT(*) as countTotal,
SUM(`cost`) as cost,
(SELECT COUNT(*) FROM `purchase` WHERE `paymentType` = 'credit') as count_credit,
(SELECT COUNT(*) FROM `purchase` WHERE `paymentType` = 'cash') as count_cash
FROM `purchase` WHERE `date` >= '2011-5-4'
UPDATE2
使用ypercubes回答以下。
我可能在這裏錯了,但我不認爲你可以在'COUNT'函數中使用像'paymentType ='credit''這樣的表達式。我想你需要使用子查詢(或用戶定義的函數)來代替。 – 2011-05-05 16:10:11
如果你有100行滿足日期標準,其中50個是paymentType ='credit',你會期望結果'100,X,50'其中'X'是所有100行的總和嗎?或者'X'只是信用支付的總和? – 2011-05-05 16:13:11
如果我總共有100行50信用和50現金全部1美元,我想說的是: 100,100,50,50 – mrkmg 2011-05-05 16:14:15