MySQL SUM() COUNT()

來源:互聯網
上載者:User

標籤:create   count   null   記錄   

如題MySQL SUM() COUNT() 也是在工作中碰到的 特此記錄一下

SUM()返回的是列的數字和 如一個學生的總分數

eg:select sum(obj) from table 返回的肯定是table表中 obj列的數字之和

COUNT() 計算指定列的個和 如一個學生的不及格科目幾科

eg:select count(*) from table 返回的肯定是table表中有多少行 null是被忽略的

sum經常和group by一起使用 count經常和distinct使用


比如下邊的這個學生表

ysql> show create table SC\G
*************************** 1. row ***************************
       Table: SC
Create Table: CREATE TABLE `SC` (
  `Sid` varchar(10) DEFAULT NULL,
  `Cid` varchar(10) DEFAULT NULL,
  `score` decimal(18,1) DEFAULT NULL,
  KEY `Sid` (`Sid`),
  KEY `Cid` (`Cid`),
  CONSTRAINT `SC_ibfk_1` FOREIGN KEY (`Sid`) REFERENCES `Student` (`Sid`),
  CONSTRAINT `SC_ibfk_2` FOREIGN KEY (`Cid`) REFERENCES `Course` (`Cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> select * from SC;
+------+------+-------+
| Sid  | Cid  | score |
+------+------+-------+
| 01   | 01   |  80.0 |
| 01   | 02   |  90.0 |
| 01   | 03   |  99.0 |
| 02   | 01   |  70.0 |
| 02   | 02   |  60.0 |
| 02   | 03   |  80.0 |
| 03   | 01   |  80.0 |
| 03   | 02   |  80.0 |
| 03   | 03   |  80.0 |
| 04   | 01   |  50.0 |
| 04   | 02   |  30.0 |
| 04   | 03   |  20.0 |
| 05   | 01   |  76.0 |
| 05   | 02   |  87.0 |
| 06   | 01   |  31.0 |
| 06   | 03   |  34.0 |
| 07   | 02   |  89.0 |
| 07   | 03   |  98.0 |
+------+------+-------+
18 rows in set (0.01 sec)

mysql>

統計一下成績不及格的Sid及總成績

1 select sid,sum(score) from SC where score < 60 group by sid ;

2 select sid,count(score) from SC where score < 60 group by sid;

執行結果:


mysql> select sid,sum(score) from SC where score < 60 group by sid;
+------+------------+
| sid  | sum(score) |
+------+------------+
| 04   |      100.0 |
| 06   |       65.0 |
+------+------------+
2 rows in set (0.03 sec)

mysql> select sid,count(score) from SC where score < 60 group by sid;
+------+--------------+
| sid  | count(score) |
+------+--------------+
| 04   |            3 |
| 06   |            2 |
+------+--------------+
2 rows in set (0.00 sec)

mysql>
也就是說 下面可以看出區別

Sum()函數裡面的參數是列名的時候 是計算資料行名的值的相加

Count()函數裡面的參數是列名的的時候,那麼會計算有值項的次數

mysql> select sid,sum(score < 60) from SC group by sid;
+------+-----------------+
| sid  | sum(score < 60) |
+------+-----------------+
| 01   |               0 |
| 02   |               0 |
| 03   |               0 |
| 04   |               3 |
| 05   |               0 |
| 06   |               2 |
| 07   |               0 |
+------+-----------------+
7 rows in set (0.01 sec)

mysql> select sid,count(score < 60) from SC group by sid;   
+------+-------------------+
| sid  | count(score < 60) |
+------+-------------------+
| 01   |                 3 |
| 02   |                 3 |
| 03   |                 3 |
| 04   |                 3 |
| 05   |                 2 |
| 06   |                 2 |
| 07   |                 2 |
+------+-------------------+
7 rows in set (0.02 sec)

mysql>


本文出自 “營運邦” 部落格,請務必保留此出處http://aklaus.blog.51cto.com/9724632/1597488

MySQL SUM() COUNT()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.