join - How to show last commented post in mysql where are two tables involved? (post and comments) -


i have internal site forum. simple 1 has 2 tables: 1 forum posts , forum contents.

i show list of posts , when user clicks on each item goes post comments.

the forum posts ordered post date (in descending order), order update date, not new posts but new commented posts appear @ top.

these 2 tables:

create table foro (     foroid int unsigned not null auto_increment primary key,     userid int(11) not null,     estado char(7) not null default 'abierto',     asunto char(200) null,     fecha char(25) null,     deprecated_userid int(11) null,     deprecated_nodeid int(11) null,     texto text null ) engine=innodb default charset=utf8;  create table forocomentarios (     comentarioid int unsigned not null auto_increment primary key,     foroid int(11) not null,     userid int(11) not null,     fecha char(25) null,     deprecated_userid int(11) null,     deprecated_nodeid int(11) null,     texto text null ) engine=innodb default charset=utf8; 

and here how show forum posts now:

    select  foro.foroid,              foro.userid userid,              foro.asunto,              foro.estado,              foro.fecha,             usuarios.userid, usuarios.docfoto,             usuarios.userapellido, usuarios.usernombres     foro join usuarios     on foro.userid = usuarios.userid     order foro.fecha desc 

i think should use subqueries that, i've tried several ways , thing list of duplicated posts. is there way achieve without modifying table's structures?

trying create proper mcve question, i've found solution :)

so, in case helps someday, worked me:

    select distinct foro.foroid,              foro.userid userid,              foro.asunto,              foro.estado,              usuarios.userid, usuarios.docfoto,             usuarios.userapellido, usuarios.usernombres,             foro.fecha fechap,             forocomentarios.fecha fechac,             ifnull(group_concat(                     forocomentarios.fecha                      order forocomentarios.fecha                      desc                      separator ','),                     foro.fecha) fechax     foro join usuarios     on foro.userid = usuarios.userid     left join forocomentarios     on foro.foroid = forocomentarios.foroid     group foro.foroid     order fechax desc 

after take fechax field (explode latest unix time number), , use order list.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -