我有一个已发布的字段,其值为1或0.我希望能够提醒用户有多少记录需要发布或未发布。我想计算published
等于0
的表格推荐中的记录数量,并将该值输出为
Awaiting to be published:#with published=0
我有这个,我意识到这是错误的:
$sql = mysql_query("SELECT * FROM testimonials WHERE published='0'");
$result=mysql_num_rows($sql);
echo $result;
答案 0 :(得分:2)
$sql = mysql_query("SELECT SUM(published=0) AS nb_unpublished,
SUM(published=1) AS nb_published FROM testimonials");
$result = mysql_fetch_assoc($sql);
echo "Records awaiting to be published:" . $result['nb_unpublished'] . '<br>';
echo "Records already published:" . $result['nb_published'] . '<br>';
答案 1 :(得分:0)
你可以用这个:
SELECT SUM(IF(published='0',1,0)) AS 'count_awaiting',
SUM(IF(published='1',1,0)) AS 'count_publish'
FROM testimonials
#count_awaiting is number of records with published=0
#count_publish is number of records with published=1
答案 2 :(得分:0)
这是对的。你也可以找到这样的记录数:
$sql = mysql_query("SELECT COUNT(*) AS `rows` FROM `testimonials` WHERE `published`='0'");
$result=mysql_fecth_array($sql);
echo "Awaiting to be published:".$result['rows'];