How to get the result count and showmeta in sphinxql _ MySQL

Source: Internet
Author: User
How does sphinxql obtain the number of results and detailed description of showmeta bitsCN.com

Mysql:
Select count (*) from main_index;

However, a syntax error is reported here.

Method 1:
Check the document as follows:
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.

That is to say, count (*) can be used only when group by is used, for example:


Select 1 as dummy, count (*) c from main_index group by dummy;
+ ------ + -------- + ------- + -------- +
| Id | weight | dummy | @ count |
+ ------ + -------- + ------- + -------- +
| 1001 | 1 | 1 | 15659 |
+ ------ + -------- + ------- + -------- +

Method 2

Select * from main_index limit 0;
Show meta;
+ --------------- + ------- +
| Variable_name | Value |
+ --------------- + ------- +
| Total | 67 |
| Total_found | 67 |
| Time | 1, 0.001 |
| Keyword [0] | ha |
| Docs [0] | 67 |
| Hits [0] | 115 |
+ --------------- + ------- +

That is to say, use show meta to get the total_found, which is the total number of records.

Let's take a look at show meta:
Show meta shows additional meta-information about the latest query such as query time and keyword statistics:

That is to say, it displays the additional information of the last query, such as the query time, keyword statistics, and total records.

Mysql> SELECT * FROM test1 where match ('test | one | two ');
+ ------ + -------- + ---------- + ------------ +
| Id | weight | group_id | date_added |
+ ------ + -------- + ---------- + ------------ +
| 1 | 3563 | 456 | 1231721236 |
| 2 | 2563 | 123 | 1231721236 |
| 4 | 1480 | 2 | 1231721236 |
+ ------ + -------- + ---------- + ------------ +
3 rows in set (0.01 sec)

Mysql> show meta;
+ --------------- + ------- +
| Variable_name | Value |
+ --------------- + ------- +
| Total | 3 |
| Total_found | 3 |
| Time | 1, 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 |
+ --------------- + ------- +
12 rows in set (0.00 sec)

How to call in PHP?

// Obtain the total number of records
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;
}
?>

Note: If the code uses multiple database connections, the corresponding conn must be passed in; otherwise, the result cannot be obtained.

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.