熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> MySQL >> 正文

sphinxql如何得到結果數及show meta的詳細說明

2022-06-13   來源: MySQL 

  mysql
select count(*) from main_index;

  但是這個在這裡卻報語法錯誤

  第一種方法
查文檔得
Aggregate functions (AVG() MIN() MAX() SUM()) in column list clause are supported Arguments to aggregate functions can be either plain attributes or arbitrary expressions COUNT(*) is implicitly supported as using GROUP BY will add @count column to result set Explicit support might be added in the future COUNT(DISTINCT attr) is supported Currently there can be at most one COUNT(DISTINCT) per query and an argument needs to be an attribute Both current restrictions on COUNT(DISTINCT) might be lifted in the future

  也就是說只有在group by的時候才能用count(*)

  

  復制代碼 代碼如下:

  select as dummycount(*) c from main_index group by dummy;
+++++
| id | weight | dummy | @count |
+++++
| |       | | |
+++++


第二種方法

  復制代碼 代碼如下:

  select * from main_index limit ;
show meta;
+++
| Variable_name | Value |
+++
| total         |  |
| total_found   |  |
| time |   |
| keyword[]     |  ha |
| docs[] |   |
| hits[] | |
+++


也就是說用show meta來得到這個total_found這個就是總記錄數

  下面我們來說一下show meta:
SHOW META shows additional metainformation about the latest query such as query time and keyword statistics:

  也就是說它顯示的是最近一次查詢附加的一些信息比如查詢時間關鍵字統計總記錄等

  復制代碼 代碼如下:

  mysql> SELECT * FROM test WHERE MATCH(test|one|two);
+++++
| id   | weight | group_id | date_added |
+++++
|    |   |      | |
|    |   |      | |
|    |   |        | |
+++++
rows in set ( sec)

mysql> SHOW META;
+++
| Variable_name | Value |
+++
| total         |      |
| total_found   |      |
| time          | |
| keyword[]    | test  |
| docs[]       |      |
| hits[]       |      |
| keyword[]    | one   |
| docs[]       |      |
| hits[]       |      |
| keyword[]    | two   |
| docs[]       |      |
| hits[]       |      |
+++
rows in set ( sec)


在PHP中如何調用?

  復制代碼 代碼如下:

  <?php
//獲取總記錄個數
private function getTotalFound($conn) {
    $sql = show meta;
    $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必須傳進來否則是取不到結果的


From:http://tw.wingwit.com/Article/program/MySQL/201405/30874.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.