dqwn64004 2015-07-17 09:39
浏览 56
已采纳

来自PHP-api的Sphinx avg功能

I have this mysql table:

CREATE TABLE person (id INT(6) AUTO_INCREMENT PRIMARY KEY,name    VARCHAR(30) NOT NULL,age int(4) NOT NULL);

I run these inserts: INSERT INTO person VALUES(null, 'one', 42); INSERT INTO person VALUES(null, 'two', 49); INSERT INTO person VALUES(null, 'three', 16);

Running:

select avg(age) from person;

gives me "35.6"

I then index these table in Sphinx. Now I want to run more or less the same query in Sphinx as the one I run in MySQL. From command line I issue this command:

select avg(age) from person_idx;

Fine. Even here I get "35.6".

Now to the problem. I need to run this query from PHP. Im using sphinxapi.php script that comes with Sphinx-release. I had a look at the docs and googled a lot. But I cannot find a way to run these kind of functions (avg, sum etc) from PHP. Is there a way to do this?

In PHP I have this code:

require('sphinxapi.php');
$client = new SphinxClient();
$res = $client->Query('', 'person_idx');   

Where do I put the "select AVG(age)" part of the sql-query? sphinxapi.php has a methods you can call. But I cannot see a way to decide what to select.

  • 写回答

1条回答 默认 最新

  • matlabmann 2015-07-17 12:21
    关注

    Add before the Query() call

    $client->setSelect("AVG(age) AS avg_age");
    

    setSelect is roughly equivalent to the 'SELECT ...' part of sphinxQL. But in general you need to use AS for all function use.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3