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
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?