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

我无法让ORDER BY在Couchbase Server 4.5中正常工作</ p>

< p> 文件</ strong> </ p>

  {“name”:“Green”,“price”:“156”} 
{“name”:“黄色 “,”“price”:“175”}
{“name”:“Red”,“price”:“1”}
{“name”:“Blue”,“price”:“18”}
</ code> </ pre>

PHP </ strong> </ p>

  $ cluster = new CouchbaseCluster('http://127.0。  0.1:8091'); 
$ bucket = $ cluster-&gt; openBucket('products');
$ bucket-&gt; enableN1ql(array('http://127.0.0.1:8093'));
$ query = CouchbaseN1qlQuery :: fromString(“SELECT * FROM products ORDER BY price ASC”);
$ results = $ bucket-&gt; query($ query);

foreach($ results as&amp; $ r){

$ name = $ r-&gt; products-&gt; name;
$ price = $ r-&gt; products-&gt; price;
echo“$ name:$ price&lt; br&gt;”;
}

</ code> </ pre>

输出</ strong> </ p>

 红色:1 
绿色:156
Yellow:175

Blue:18
</ code> </ pre>

请您告诉我怎么告诉Couch 价格字段是一个整数的基数,所以订单将正确完成。</ p>
</ div>

展开原文

原文

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个回答



假设文件在桶a中。 然后以下查询应该工作。 </ p>

 从订单中选择to_number(price)b b; 
{
“requestID”:“6642f123-08c1-4ec2-9380-c11c9fd05c60”,
“ 签名“:{
”b“:”数字“
},
”结果“:[
{
”b“:1
},
{
”b“:18
},
{
“b”:156
},
{
“b”:175
}

],
“状态”:“成功”,
“指标”:{

“elapsedTime”:“9.39578ms”,
“executionTime”:“9.36118ms”,
“resultCount”:4,
“resultSize”:125,
“sortCount”:4
}
}
</ code> </ pre>
</ div>

展开原文

原文

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
    }
}

doubo9799
doubo9799 非常感谢,我会尝试这个
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问