注意:我已经在GAE相关帖子中看到了JOIN ,但在我的网站上看到了GROUP BY 要求让我做了一个新的帖子,我已经找到了我自己的解决方案,我想检查它有多好
数据存储区实体属性和类型(数据库)
实体:PartyEntity 允许将其称为
实体:InsertEntity 允许将其称为b
目标
SELECT * FROM a JOIN b on a.partyId = b.partyIdentifier GROUP BY b.constituency;
下面我将回答我的问题,代码工作精细,建议我做任何改变或更好的方式(我只是一名学生)
答案 0 :(得分:1)
我制作的解决方案如下,建议任何更改或更好的方法
// SELECT * FROM a(PartyEntity拥有唯一的partyId) 每个partyId SELECT * FROM b
的和
private List<ListDisplay> initData() {
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
List<ListDisplay> data = new ArrayList<ListDisplay>();
Query query = new Query("PartyEntity");
PreparedQuery preparedQuery = datastore.prepare(query);
List<Entity> entities = preparedQuery.asList(FetchOptions.Builder
.withDefaults());
for (Entity entity : entities) {
String PARTY_ID = (String) entity.getProperty("partyId");
// fetch from another
Query query2 = new Query("InsertEntity");
Filter filterByPartyId = new Query.FilterPredicate(
"partyIdentifier", FilterOperator.EQUAL, PARTY_ID);
query2.setFilter(filterByPartyId);
PreparedQuery preparedQuery2 = datastore.prepare(query2);
List<Entity> entities2 = preparedQuery2.asList(FetchOptions.Builder
.withDefaults());
for (Entity entity2 : entities2) {
ListDisplay display = new ListDisplay();
display.setPartyId((String) entity.getProperty("partyId"));
display.setPartyName((String) entity.getProperty("partyName"));
display.setImage((BlobKey) entity.getProperty("image"));
display.setName((String) entity2.getProperty("name"));
display.setConstituency((String) entity2
.getProperty("constituency"));
data.add(display);
} // end loop entity2
} // end loop entity
return data;
}
// GROUP BY选区子句
public HashMap<String, List<ListDisplay>> getData() {
HashMap<String, List<ListDisplay>> data = new HashMap<>();
List<ListDisplay> list = initData();
for (ListDisplay d : list) {
// if null add
if (data.get(d.getConstituency()) == null) {
List<ListDisplay> internal = new ArrayList<>();
internal.add(d);
data.put(d.getConstituency(), internal);
}
// else modify
else {
List<ListDisplay> hashlist = data.get(d.getConstituency());
hashlist.add(d);
data.remove(d.getConstituency());
data.put(d.getConstituency(), hashlist);
}
}
return data;
}