解決mysql group by和count同時使用的問題

例如這樣一個表,我想統計email和passwords都不相同的記錄的條數spa

CREATE TABLE IF NOT EXISTS `test_users` ( 
`email_id` int(11) unsigned NOT NULL auto_increment, 
`email` char(100) NOT NULL, 
`passwords` char(64) NOT NULL, 
PRIMARY KEY (`email_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;

INSERT INTO `test_users` (`email_id`, `email`, `passwords`) VALUES 
(1, ‘jims@gmail.com', ‘1e48c4420b7073bc11916c6c1de226bb'), 
(2, ‘jims@yahoo.com.cn', ‘5294cef9f1bf1858ce9d7fdb62240546′), 
(3, ‘default@gmail.com', ‘5294cef9f1bf1858ce9d7fdb62240546′), 
(4, ‘jims@gmail.com', 」), 
(5, ‘jims@gmail.com', 」);

 

一般咱們的作法是這樣:code

SELECT COUNT(*) FROM test_users WHERE 1 = 1 GROUP BY email,passwords

 

這樣的結果是什麼呢?blog

COUNT(*) 
1 
2 
1 
1

 

顯然這不是我要的結果,這樣統計出來的是相同email和passwords的各個記錄數量之和,下面這樣就能夠了:rem

SELECT COUNT(DISTINCT email,passwords) FROM `test_users` WHERE 1 = 1
相關文章
相關標籤/搜索