中易网

php sql查询语句的count求教!

答案:4  悬赏:70  
解决时间 2021-01-13 05:33
  • 提问者网友:鼻尖触碰
  • 2021-01-12 09:25
php sql查询语句的count求教!
最佳答案
  • 二级知识专家网友:怙棘
  • 2021-01-12 10:31
不必用count(),这是相当耗资源的函数。
SQL这么写
$sql="select * from interp_images where categoryid=".$c." and actived = 1"

查询的结果用 mysql_fetch_array 赋给 $result

如果有结果 $result 为有集的数组,否则$result为空数组或者false,

所以PHP这么写即可
if( $result ) {
……
}
追问:$sql="select count(imageid) from images where categoryid=".$c." and actived = 1"

我的意思是要统计有多少条记录。这样查询后,怎么返回记录数了。
$number=count(imageid):不对的了。
追答:$sql="select count(imageid) AS c from images where categoryid=".$c." and actived = 1";
$query = mysql_query($sql);
$arrResult = mysql_fetch_row($query);
$number = $arrResult['c'];

看懂了没?
全部回答
  • 1楼网友:十鸦
  • 2021-01-12 13:12
$sql="select count(imageid) from interp_images where categoryid=".$c." and actived = 1"; 当然你也可以写成$sql="selectcount(*) from interp_images where categoryid=".$c." and actived = 1"
追问:我的意思是,在外部使用时,应该怎么写。例如:
if(count(imageid)>0){

}
这里的count怎么使用!
追答:select count(imageid) as number from interp_images where categoryid=".$c." and actived = 1"
外部就用 if($number)>0{
}
  • 2楼网友:猎心人
  • 2021-01-12 12:14
SELECt owner, COUNT(*) FROM pet GROUP BY owner;
标点符号。。。。
  • 3楼网友:轻雾山林
  • 2021-01-12 12:07
count() 了,然后再* ,就会出错了,只能写字段名了,但是出来的也不是准确的,因为count 后,结果只有一条,出来的你要的字段也就只有一条了,
所以还是分开来写吧,先count()再查*
$sql="select count(imageid) from interp_images where categoryid=".$c." and actived = 1"
$sql2="select * from interp_images where categoryid=".$c." and actived = 1"
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息