php sql查询语句的count求教!

$sql="select * count(imageid) from interp_images where categoryid=".$c." and actived = 1"
这条语句能用count判断记录存在不,能的话该怎么写!
最新回答
雨点躲日落

2024-11-07 01:15:25

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"
压抑情绪

2024-11-07 09:38:03

不必用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'];

看懂了没?
罂语

2024-11-07 01:04:22

$sql="select count(imageid) from interp_images where categoryid=".$c." and actived = 1"; 当然你也可以写成$sql="select count(*) 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{

}
っ麦↘兜兜

2024-11-07 14:46:08

SELECT owner, COUNT(*) FROM pet GROUP BY owner;

标点符号。。。。