MySQL get name of last added value on group by query -



MySQL get name of last added value on group by query -

i'm trying show in index of videogame cheats , tips page cheats available listed, i'm grouping results videogame , counting how many cheats videogame has, can accomplish, i'm trying show lastly added cheat game.

my query is:

select a_games.game_id, count(*) cheat_count, a_games.game_fname, a_games.game_logo, a_cheats.cheat_title a_cheats left bring together a_games on a_games.game_id=a_cheats.game_id grouping a_cheats.game_id

this shows first added cheat only.

i tried using max on cheat_id value cheat_title keeps showing first added cheat.

table a_cheats

cheat_id type_id member_id game_id cheat_title cheat_body cheat_date 1 | 1 | 1 | 22 | truques v...| introduz...| 2014-10-...| 2 | 1 | 1 | 25 | invulnera...| durante ...| 2014-10-...| 3 | 1 | 1 | 25 | modo debu...| durante ...| 2014-10-...| 4 | 1 | 1 | 25 | charme in...| durante ...| 2014-10-...| 5 | 1 | 1 | 36 | cabeças e...| começa o...| 2014-10-...|

table a_games

game_id genre_id member_id game_fname game_sname game_logo 22 | 15 | 1 | 4x4 worl...| | 5259da0... 25 | 3 | 1 | akuji th...| | 5287ae0... 36 | 25 | 1 | star...| | 5287daa...

so in results akuji heartless should show cheat_title "charme infinito" lastly added cheat game

query results

game_id game_count game_fname game_logo cheat_title 22 | 1 | 4x4 world trophy | 5259da0527128_ava_4x4worldtrophy.jpg | truques (vários) 25 | 3 | akuji heartless | 5287ae093e115_ava_akujiheartless.jpg | invulnerabilidade 36 | 1 | star tennis'99 | 5287daa2695ef_ava_allstartennis99.jpg| cabeças e pés grandes

it seems 1 time phone call count(), can't command title pick later on (it straight gets title of earliest entry). thus, can want calling count later.

note: can alter order a_cheats.cheat_id desc order a_cheats.cheat_date desc if want more reliable.

class="lang-sql prettyprint-override">select gameid,gamename,cheattitle, count(gameid) cheat_count ( select a_games.game_id gameid, a_games.game_fname gamename, a_cheats.cheat_title cheattitle a_cheats bring together a_games on a_games.game_id=a_cheats.game_id order a_cheats.cheat_id desc ) ungrouped grouping gameid

sqlfiddle: http://sqlfiddle.com/#!2/63f888/18

or

select gameid,gamename,cheattitle, count(cheatid) cheat_count ( select a_games.game_id gameid, a_games.game_fname gamename, a_cheats.cheat_title cheattitle, a_cheats.cheat_id cheatid a_cheats right bring together a_games on a_games.game_id=a_cheats.game_id order a_cheats.cheat_id desc ) ungrouped grouping gameid

sqlfiddle: http://sqlfiddle.com/#!2/63f888/19

mysql

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -