MySQL,计数“填充”的数目。字段 [英] MySQL, count the number of "filled" fields in a table

查看:187
本文介绍了MySQL,计数“填充”的数目。字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySQL(表):

+----+------+
| id | text |
+----+------+
| 1  |      |
+----+------+
| 2  | blah |
+----+------+
| 3  |      |
+----+------+
| 4  | blah |
+----+------+
| 5  | blah |
+----+------+



PHP:

PHP:

$a = mysql_query("SELECT COUNT(*) AS count1 FROM `table`");
$b = mysql_fetch_assoc($a);

echo $b['count1'];

输出:

5

但是,我还要计算

However, I also want to count the text fields which are filled - within the same query, if possible.

结果:

5 in total
3 with filled text fields


推荐答案

SELECT COUNT(*) AS `total`, SUM(IF(`text` <> "",1,0)) AS `non_empty` FROM `table`

这篇关于MySQL,计数“填充”的数目。字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆