Skip to content

Instantly share code, notes, and snippets.

@naichilab
Created June 9, 2016 17:51
Show Gist options
  • Save naichilab/7249a042625697c547b924ec879c943f to your computer and use it in GitHub Desktop.
Save naichilab/7249a042625697c547b924ec879c943f to your computer and use it in GitHub Desktop.
NCMBでunion all的なことができないか試した
//欲しいデータ
//SQLで書くとこんなかんじ。これをNCMBで再現したい。
//select level,time from TestClass where level = 2 order by time desc limit 3
//union all
//select level,time from TestClass where level = 3 order by time desc limit 3
//union all
//select level,time from TestClass where level = 4 order by time desc limit 3
//union all
//select level,time from TestClass where level = 5 order by time desc limit 3
//union all
//select level,time from TestClass where level = 6 order by time desc limit 3;
//結果:Orで結合できるのはあくまでWhere条件のみなので、orderbyやlimitが入っていると結合できないみたい?
using UnityEngine;
using NCMB;
using System.Collections.Generic;
using System;
public class ncmbtest : MonoBehaviour
{
public void Send ()
{
//以下を50回押して250レコード作成したデータがある
// for (int lv = 2; lv <= 6; lv++) {
// float time = Random.Range (0f, 1f) * 60.0f;
// NCMBObject testClass = new NCMBObject ("TestClass");
// testClass ["level"] = lv;
// testClass ["time"] = time;
// testClass.SaveAsync ();
// }
//各レベルでTop3を取得するクエリ
List<NCMBQuery<NCMBObject>> queries = new List<NCMBQuery<NCMBObject>> ();
queries.Add (CreateTop3Query (2));
queries.Add (CreateTop3Query (3));
queries.Add (CreateTop3Query (4));
queries.Add (CreateTop3Query (5));
queries.Add (CreateTop3Query (6));
//全クエリをor結合
NCMBQuery<NCMBObject> mainQuery = NCMBQuery<NCMBObject>.Or (queries);
mainQuery.FindAsync ((List<NCMBObject> objList, NCMBException e) => {
if (e != null) {
//検索失敗時の処理
Debug.Log ("失敗");
} else {
//Scoreが7のオブジェクトを出力
foreach (NCMBObject obj in objList) {
Debug.Log (string.Format ("level:{0}, time:{1}, objId:{2}", obj ["level"], obj ["time"], obj.ObjectId));
}
}
});
}
private NCMBQuery<NCMBObject> CreateTop3Query (int level)
{
NCMBQuery<NCMBObject> query = new NCMBQuery<NCMBObject> ("TestClass");
query.WhereEqualTo ("level", level);
// query.OrderByDescending ("time"); //これを指定するとOrメソッドで結合できないっぽい?
// query.Limit = 3; //
return query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment