zl程序教程

您现在的位置是:首页 >  其它

当前栏目

sphinxql如何得到结果数及showmeta的详细说明

如何 详细 说明 结果 得到
2023-06-13 09:14:45 时间

mysql:
selectcount(*)frommain_index;

但是这个在这里却报语法错误。

第一种方法:
查文档得:
Aggregatefunctions(AVG(),MIN(),MAX(),SUM())incolumnlistclausearesupported.Argumentstoaggregatefunctionscanbeeitherplainattributesorarbitraryexpressions.COUNT(*)isimplicitlysupportedasusingGROUPBYwilladd@countcolumntoresultset.Explicitsupportmightbeaddedinthefuture.COUNT(DISTINCTattr)issupported.CurrentlytherecanbeatmostoneCOUNT(DISTINCT)perqueryandanargumentneedstobeanattribute.BothcurrentrestrictionsonCOUNT(DISTINCT)mightbeliftedinthefuture.

也就是说只有在groupby的时候才能用count(*),如:

复制代码代码如下:


select1asdummy,count(*)cfrommain_indexgroupbydummy;
+------+--------+-------+--------+
|id|weight|dummy|@count|
+------+--------+-------+--------+
|1001|1     |1|15659|
+------+--------+-------+--------+

第二种方法
复制代码代码如下:

select*frommain_indexlimit0;
showmeta;
+---------------+-------+
|Variable_name|Value|
+---------------+-------+
|total        | 67|
|total_found  | 67|
|time|0.001 |
|keyword[0]    | ha|
|docs[0]|67 |
|hits[0]|115|
+---------------+-------+

也就是说用showmeta来得到这个total_found,这个就是总记录数。

下面我们来说一下showmeta:
SHOWMETAshowsadditionalmeta-informationaboutthelatestquerysuchasquerytimeandkeywordstatistics:

也就是说它显示的是最近一次查询附加的一些信息,比如查询时间、关键字统计、总记录等。

复制代码代码如下:
mysql>SELECT*FROMtest1WHEREMATCH("test|one|two");
+------+--------+----------+------------+
|id  |weight|group_id|date_added|
+------+--------+----------+------------+
|   1|  3563|     456|1231721236|
|   2|  2563|     123|1231721236|
|   4|  1480|       2|1231721236|
+------+--------+----------+------------+
3rowsinset(0.01sec)

mysql>SHOWMETA;
+---------------+-------+
|Variable_name|Value|
+---------------+-------+
|total        |3    |
|total_found  |3    |
|time         |0.005|
|keyword[0]   |test |
|docs[0]      |3    |
|hits[0]      |5    |
|keyword[1]   |one  |
|docs[1]      |1    |
|hits[1]      |2    |
|keyword[2]   |two  |
|docs[2]      |1    |
|hits[2]      |2    |
+---------------+-------+
12rowsinset(0.00sec)

在PHP中如何调用?
复制代码代码如下:
<?php
//获取总记录个数
privatefunctiongetTotalFound($conn){
   $sql="showmeta";
   $total_result=@mysql_query($sql,$conn);
   $totals=array();
   while(($row=mysql_fetch_assoc($total_result))!==false){
       $totals[$row["Variable_name"]]=$row["Value"];
   }
   return$totals;
}
?>

注意:如果代码中用了多个数据库连接的话,这个相应的conn必须传进来,否则是取不到结果的。