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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)