AlaSQL可以使用DISTINCT和SEARCH

时间:2017-03-10 00:29:29

标签: alasql

假设我有一个如下所示的数组:

var data = [{
  "categories": [{
    "categoryName": "‌belt‌",
    "categoryValue": "‌white‌"
  }, {"categoryName": "‌level‌", "categoryValue": "‌Beginner‌"}, {
    "categoryName": "‌type‌",
    "categoryValue": "‌technique‌"
  }], "id": 1, "skillName": "‌Beginner 1‌", "skips": 0, "weight": 1
}, {
  "categories": [{"categoryName": "‌belt‌", "categoryValue": "‌white‌"}, {
    "categoryName": "‌level‌",
    "categoryValue": "‌Beginner‌"
  }, {"categoryName": "‌type‌", "categoryValue": "‌technique‌"}],
  "id": 2,
  "skillName": "‌Beginner 2‌",
  "skips": 0,
  "weight": 1
}, {
  "categories": [{"categoryName": "‌belt‌", "categoryValue": "‌white‌"}, {
    "categoryName": "‌level‌",
    "categoryValue": "‌Beginner‌"
  }, {"categoryName": "‌type‌", "categoryValue": "‌technique‌"}],
  "id": 3,
  "skillName": "‌Beginner 3‌",
  "skips": 0,
  "weight": 1
}, {
  "categories": [{"categoryName": "‌belt‌", "categoryValue": "‌white‌"}, {
    "categoryName": "‌level‌",
    "categoryValue": "‌Beginner‌"
  }, {"categoryName": "‌type‌", "categoryValue": "‌technique‌"}],
  "id": 4,
  "skillName": "‌Beginner 4‌",
  "skips": 0,
  "weight": 1
}, {
  "categories": [{"categoryName": "‌belt‌", "categoryValue": "‌white‌"}, {
    "categoryName": "‌level‌",
    "categoryValue": "‌Beginner‌"
  }, {"categoryName": "‌type‌", "categoryValue": "‌technique‌"}],
  "id": 5,
  "skillName": "‌Beginner 5‌",
  "skips": 0,
  "weight": 1
},
...
{
  "categories": [{"categoryName": "‌belt‌", "categoryValue": "‌orange‌"}, {
    "categoryName": "‌level‌",
    "categoryValue": "‌Intermediate‌"
  }, {"categoryName": "‌type‌", "categoryValue": "‌technique‌"}],
  "id": 13,
  "skillName": "‌Intermediate 3‌",
  "skips": 0,
  "weight": 1
}, {
  "categories": [{"categoryName": "‌belt‌", "categoryValue": "‌orange‌"}, {
    "categoryName": "‌level‌",
    "categoryValue": "‌Intermediate‌"
  }, {"categoryName": "‌type‌", "categoryValue": "‌technique‌"}],
  "id": 14,
  "skillName": "‌Intermediate 4‌",
  "skips": 0,
  "weight": 1
}, {
  "categories": [{"categoryName": "‌belt‌", "categoryValue": "‌orange‌"}, {
    "categoryName": "‌level‌",
    "categoryValue": "‌Intermediate‌"
  }, {"categoryName": "‌type‌", "categoryValue": "‌technique‌"}],
  "id": 15,
  "skillName": "‌Intermediate 5‌",
  "skips": 0,
  "weight": 1
}, {
  "categories": [{"categoryName": "‌belt‌", "categoryValue": "‌purple‌"}, {
    "categoryName": "‌level‌",
    "categoryValue": "‌Intermediate‌"
  }, {"categoryName": "‌type‌", "categoryValue": "‌technique‌"}],
  "id": 16,
  "skillName": "‌Intermediate 6‌",
  "skips": 0,
  "weight": 1
}
...
]

我们想要查询一下,例如:

alasql('SEARCH /categories/categoryValue WHERE(categoryName="belt") FROM ?', [data]);

这可能会给你一些类似的东西:

['white', 'white', 'white',...'orange', 'orange',...'purple'...]

我想要的是:

['white', 'orange', 'purple']

有没有办法在查询方面执行此操作,或者在返回后是否需要将其过滤为唯一值?

1 个答案:

答案 0 :(得分:0)

根据this testthis test(第85行),语法应为:

alasql('SEARCH DISTINCT(/categories/ WHERE (categoryName="belt") categoryValue) FROM ?', [data]);