在需要输出网站用户注册数,或者插入数据之前判断是否有重复记录的时候,就需要获取满足条件的MySQL查询的记录数目。 第一种方法:查询时候直接统计 123 $sql = "SELECT COUNT(*) AS count FROM TABLE WHERE id=' $id '" ; $result = mysql_fetch_array ( my
在需要输出网站用户注册数,或者插入数据之前判断是否有重复记录的时候,就需要获取满足条件的MySQL查询的记录数目。
第一种方法:查询时候直接统计
1 2 3 로그인 후 복사 로그인 후 복사 |
<span>$sql</span><span>=</span><span>"SELECT COUNT(*) AS count FROM TABLE WHERE id='<span>$id</span>'"</span><span>;</span> <span>$result</span><span>=</span><span>mysql_fetch_array</span><span>(</span><span>mysql_query</span><span>(</span><span>$sql</span><span>)</span><span>)</span><span>;</span> <span>$count</span><span>=</span><span>$result</span><span>[</span><span>'count'</span><span>]</span><span>;</span> 로그인 후 복사 |
第二种方法:先取出,后统计
1 2 3 로그인 후 복사 로그인 후 복사 |
<span>$sql</span><span>=</span><span>"SELECT * FROM TABLE WHERE id='<span>$id</span>'"</span><span>;</span> <span>$result</span><span>=</span><span>mysql_fetch_array</span><span>(</span><span>mysql_query</span><span>(</span><span>$sql</span><span>)</span><span>)</span><span>;</span> <span>$count</span><span>=</span><span>count</span><span>(</span><span>$result</span><span>)</span><span>;</span><span>//或者$count=mysql_num_rows($result);</span> 로그인 후 복사 |
不过直接使用MySQL的COUNT(*)在数据量庞大的时候,效率的优势是十分显著的,因为后者需要二次计算,所以还是最好使用前者进行数据条数的统计。
source url:http://www.benben.cc/blog/?p=345