ActiveJDBC select 来自多个表的聚合记录
ActiveJDBC select record from multiple tables with an aggregate
情况是这样的。我有一个 table 具有多个多对多关系。
game (id, name, year_release ...)
game_price (game_id, price_id, amount)
price(id, label -- new game, used game)
category(id, name)
game_category(game_id, category_id)
现在我想要 select 20 款游戏的列表,每款游戏都有价格列表和类别列表。
在SQL我会做这样的事情
select game.id, game.name string_agg(price.label||':'game_price.amount), string_agg(category.name)
from game
left join game_price gp on (game.id = gp.game_id)
left join price on (price.id = price_id)
left join game_category gc on (game.id = gc.game_id)
left join category on (category.id = category_id)
group by game.id
我这里有一个例子:http://javalite.io/lazy_and_eager 一对多但没有多对多。
我是否必须像这样先加载所有 game_price 然后加载所有 game_category?
List<GamesPrices> gamesPrices = GamesPrices.findAll(Game.class, Price.class)
List<GamesCategories> gamesCategories = GamesCategories.findAll(Game.class, Category.class)
但是我怎样才能循环获取每场比赛的所有信息呢?
有两种方法:
有模特:
List<Game> games = Game.where(criteria, param1, param2).include(Price.class, Category,class).limit(20);
for(Game g: games){
List<Price> prices = g.getAll(Price.class);
List<Category> categories = g.getAll(Category.class);
}
没有模特:
String sql = "select game.id, game.name string_agg(price.label||':'game_price.amount), string_agg(category.name)" +
"from game" +
"left join game_price gp on (game.id = gp.game_id)" +
"left join price on (price.id = price_id)" +
"left join game_category gc on (game.id = gc.game_id)" +
"left join category on (category.id = category_id)" +
"group by game.id";
List<Map> results = Base.findAll(sql);
希望对您有所帮助!
情况是这样的。我有一个 table 具有多个多对多关系。
game (id, name, year_release ...)
game_price (game_id, price_id, amount)
price(id, label -- new game, used game)
category(id, name)
game_category(game_id, category_id)
现在我想要 select 20 款游戏的列表,每款游戏都有价格列表和类别列表。
在SQL我会做这样的事情
select game.id, game.name string_agg(price.label||':'game_price.amount), string_agg(category.name)
from game
left join game_price gp on (game.id = gp.game_id)
left join price on (price.id = price_id)
left join game_category gc on (game.id = gc.game_id)
left join category on (category.id = category_id)
group by game.id
我这里有一个例子:http://javalite.io/lazy_and_eager 一对多但没有多对多。
我是否必须像这样先加载所有 game_price 然后加载所有 game_category?
List<GamesPrices> gamesPrices = GamesPrices.findAll(Game.class, Price.class)
List<GamesCategories> gamesCategories = GamesCategories.findAll(Game.class, Category.class)
但是我怎样才能循环获取每场比赛的所有信息呢?
有两种方法:
有模特:
List<Game> games = Game.where(criteria, param1, param2).include(Price.class, Category,class).limit(20);
for(Game g: games){
List<Price> prices = g.getAll(Price.class);
List<Category> categories = g.getAll(Category.class);
}
没有模特:
String sql = "select game.id, game.name string_agg(price.label||':'game_price.amount), string_agg(category.name)" +
"from game" +
"left join game_price gp on (game.id = gp.game_id)" +
"left join price on (price.id = price_id)" +
"left join game_category gc on (game.id = gc.game_id)" +
"left join category on (category.id = category_id)" +
"group by game.id";
List<Map> results = Base.findAll(sql);
希望对您有所帮助!