dshxbrzgi090608692 2016-02-26 16:26
浏览 86
已采纳

Couchbase Server 4.5 N1QL按整数字段排序(PHP SDK)

I am unable to get the ORDER BY to work correctly in Couchbase Server 4.5

Documents

{ "name": "Green", "price": "156" }
{ "name": "Yellow", "price": "175" }
{ "name": "Red", "price": "1" }
{ "name": "Blue", "price": "18" }

PHP

$cluster = new CouchbaseCluster('http://127.0.0.1:8091');
$bucket  = $cluster->openBucket('products');
$bucket->enableN1ql(array('http://127.0.0.1:8093'));
$query = CouchbaseN1qlQuery::fromString("SELECT * FROM products ORDER BY price ASC");
$results = $bucket->query($query);

foreach ($results as &$r) { 
   $name  = $r->products->name;
   $price = $r->products->price;
   echo "$name: $price <br>";
}

Output

Red: 1
Green: 156
Yellow: 175
Blue: 18

Please will you let me know how I can tell Couchbase that the price field is an integer so the order will be done correctly.

  • 写回答

1条回答

  • dqbn76906 2016-02-26 18:45
    关注

    Assume the documents are in bucket a. Then the following query should work.

    select to_number(price) b from a order by b;
    {
        "requestID": "6642f123-08c1-4ec2-9380-c11c9fd05c60",
        "signature": {
        "b": "number"
        },
        "results": [
            {
                "b": 1
            },
            {
                "b": 18
            },
            {
                "b": 156
            },
            {
                "b": 175
            }
        ],
        "status": "success",
        "metrics": {
            "elapsedTime": "9.39578ms",
            "executionTime": "9.36118ms",
            "resultCount": 4,
            "resultSize": 125,
            "sortCount": 4
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的