Forum OpenACS Q&A: Response to Need some help on a query!

Collapse
Posted by Ola Hansson on
Jonathan, thanks to you the query is *much* quicker than the one I tried before. If you come to my site when it is released you can count on rolling a lot of sixes😉 Here's the final version:
set query "select ma.*,
    ga.game_id,
    mo.player_id,
    mo.move_annotation as latest_move,
    ua.first_names || ' ' || ua.last_name as player_a_name,
    ub.first_names || ' ' || ub.last_name as player_b_name
from bg_matches ma,bg_games ga,bg_moves mo,users ua,users ub
where ga.match_id = ma.match_id
    and ga.game_id = (select max(ga2.game_id) from bg_games ga2 where ga2.match_id = ma.match_id)
    and mo.game_id = ga.game_id
    and mo.move_id = (select max(mo2.move_id) from bg_moves mo2 where mo2.game_id = ga.game_id)
    and (ma.player_a = 4 or ma.player_b = 4)
    and ma.player_a = ua.user_id
    and ma.player_b = ub.user_id
$extra_where_clause
order by ma.match_id desc"