使用GROUP BY的時候如何統計記錄條數 COUNT(*) DISTINCT_PHP教程

來源:互聯網
上載者:User
例如這樣一個表,我想統計email和passwords都不相同的記錄的條數
複製代碼 代碼如下:
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', ”);

通常我們的做法是這樣
複製代碼 代碼如下:
SELECT COUNT(*) FROM test_users WHERE 1 = 1 GROUP BY email,passwords

這樣的結果是什麼呢?
複製代碼 代碼如下:
COUNT(*)
1
2
1
1

顯然這不是我要的結果,這樣統計出來的是相同email和passwords的各個記錄數量之和,下面這樣就可以了
複製代碼 代碼如下:
SELECT COUNT(DISTINCT email,passwords) FROM `test_users` WHERE 1 = 1

當然在php裡面也可以用mysql_num_rows來擷取記錄的條數,但是這樣的效率不高,可以參考這篇文章
mysql_num_rows VS COUNT 效率問題分析

http://www.bkjia.com/PHPjc/323248.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323248.htmlTechArticle例如這樣一個表,我想統計email和passwords都不相同的記錄的條數 複製代碼 代碼如下: CREATE TABLE IF NOT EXISTS `test_users` ( `email_id` int(11) unsigned...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.