MySQL latest records with condition -


i need latest records, repeated more 2 times.

structure:

create table if not exists `tags` (   `tag_n` int(10) not null auto_increment,   `post_n` int(10) not null,   `tag` varchar(30) collate utf8_bin default null,   primary key (`tag_n`),   key `tag` (`tag`),   key `post_n` (`post_n`), ) engine=myisam  default charset=utf8 collate=utf8_bin; 

records:

select * tags order post_n desc limit 0 , 30 

alt text

my query:

select tag, count(post_n) tags_count  tags group tag having tags_count>=2 order post_n desc limit 5 

alt text

but wrong results, latest must "xpro", can't understand what`s wrong.

any ideas?

p.s. sorry english.

version 1

select tag, count(post_n) tags_count ,max(post_n) max_post_n tags group tag having tags_count>=2 order max_post_n desc limit 5 

version 2 faster select slower insert. stats updates online

create table if not exists `tags` (   `tag_n` int(10) not null auto_increment,   `post_n` int(10) not null,   `tag` varchar(30) collate utf8_bin default null,   primary key (`tag_n`),   key `tag` (`tag`),   key `post_n` (`post_n`), ) engine=innodb  default charset=utf8 collate=utf8_bin;  create table if not exists `tags_stats` (   `tag` varchar(30),   `tags_count` int(10) not null,   `max_post_n` int(10) not null,   primary key (`tag`)  ) engine=innodb  default charset=utf8 collate=utf8_bin;   pseudo code :  insert tags(tag,post_n) values(tag_value, post_n_value);  row = select * tags_stats tag=tag_value; if not row:     insert tags_stats(tag,tags_count,max_post_n) values(tag_value,1,post_n_value); else:     if row.max_post_n < post_n_value         update tags_stats set tags_count=tags_count+1,           max_post_n=post_n_value tag=tag_value;     else:         update tags_stats set tags_count=tags_count+1,           tag=tag_value; ####################################### select * tags_stats order max_post_n desc; 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -