我是mySQL的新手,因此我只是在寻找一个非常简单的COUNT查询,我没有找到任何真正清晰的在线解释。
我要做的是
COUNT我的PASSENGER表中groupID = 0的行数,然后能够回显计数将返回的数值,我该怎么做?
答案 0 :(得分:4)
像这样的东西
$query = "SELECT COUNT(*) c FROM PASSENGER WHERE groupID = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['c']; //Here is your count
答案 1 :(得分:2)
//get result
$data = mysql_query("SELECT count(*) as total FROM PASSENGER WHERE groupID = 0");
$info = mysql_fetch_assoc($data);
//output result
echo "Rows found :" . $info["total"];
答案 2 :(得分:2)
$query = "SELECT COUNT(*) as result FROM passenger WHERE groupID = 0";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo "Number is: ", $row;
答案 3 :(得分:0)
SELECT COUNT(*) FROM PASSENGER WHERE GROUPID = 0;
答案 4 :(得分:0)
其他答案总结得很漂亮。只是想添加,如果您以后在脚本中实际需要乘客表中的数据,那么谨慎的做法是不要进行两次单独的调用以获取计数和数据。最好的方法是:
<?php
$query = "SELECT * FROM PASSENGER WHERE groupID=0";
$result = mysql_query($query);
$result_count = mysql_num_rows($result);
?>
与:SELECT PASSENGER.*, COUNT(*) as passenger_count FROM PASSENGER WHERE groupID=0