2011-10-10 77 views
0

我有兩個疑問:兩個查詢優化

SELECT `type`, COUNT(`id`) AS 'type' FROM `items` WHERE `post` = `id` GROUP BY `type`; 
SELECT `type`, COUNT(`post`!=`id`) AS 'posts' FROM `items` GROUP BY `type`; 

有沒有什麼辦法讓它在一個查詢?

回答

1

如何像這樣

SELECT `type`, 
     COUNT(`post`=`id`), 
     COUNT(`post`!=`id`) AS 'posts' 
FROM `items` 
GROUP BY `type`; 
+0

查詢是確定的,但這樣的:!'COUNT('POST' ='id'),'返回完全相同的行值'COUNT('POST' = 'id')AS'posts''它不應該是這樣的,因爲'COUNT('post' ='id'),'這應該在第一行返回'5',但它返回'20',與此相同:'COUNT('post'!='id')AS'posts''。 – Isarius

+0

你應該使用'SUM()'而不是'COUNT()'。 –

+0

YE,這更有意義,可能在這兩種情況下? –