MySQL (表):
+----+------+
| id | text |
+----+------+
| 1 | |
+----+------+
| 2 | blah |
+----+------+
| 3 | |
+----+------+
| 4 | blah |
+----+------+
| 5 | blah |
+----+------+PHP:
$a = mysql_query("SELECT COUNT(*) AS count1 FROM `table`");
$b = mysql_fetch_assoc($a);
echo $b['count1'];输出:
5但是,如果可能的话,我也希望计算文本字段,这些字段是填充的--在同一个查询中。
结果:
5 in total
3 with filled text fields发布于 2011-01-18 23:54:00
SELECT COUNT(*) AS `total`, SUM(IF(`text` <> "",1,0)) AS `non_empty` FROM `table`发布于 2011-01-18 23:53:06
这可以通过子查询很好地完成。
SELECT COUNT(id) AS id, COUNT(SELECT text FROM 'table' WHERE text IS NOT NULL) AS t FROM 'table'给自己的注意:在提交之前开始校对你的作品。
https://stackoverflow.com/questions/4730370
复制相似问题