用COUNT函数:
mysql> SELECT count(one) FROM tb_test;
+------------+
| count(http://www.amjmh.com/v/BIBRGZ_558768/) |
+------------+
| 3 |
+------------+
1 row in set
mysql> SELECT count(two) FROM tb_test;
+------------+
| count(two) |
+------------+
| 2 |
+------------+
1 row in set
mysql> SELECT count(*) FROM tb_test;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
使用IFNULL函数:
mysql> SELECT IFNULL(one,111111111) from tb_test WHERE one = '';
+-----------------------+
| IFNULL(one,111111111) |
+-----------------------+
| |
+-----------------------+
1 row in set
mysql> SELECT IFNULL(two,11111111) from tb_test where two is NULL;
+----------------------+
| IFNULL(two,11111111) |
+----------------------+
| 11111111 |
+----------------------+
1 row in set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
小总结:使用 COUNT(字段) 统计会过滤掉 NULL 值,但是不会过滤掉空值。
说明:IFNULL有两个参数。 如果第一个参数字段不是NULL,则返回第一个字段的值。 否则,IFNULL函数返回第二个参数的值(默认值)。
---------------------