mysql - Ordering by top commented -
how list page of top commented pages on site php , mysql?
the database set sort of this:
page_id | username | comment | date_submitted --------+----------+---------+--------------- 1 | bob | hello | current date 1 | joe | byebye | current date 4 | joe | stuff | date 3 | mark | | date
how query orders them top commented pages?
here simple query start (with xxx
being areas think need with):
$querycomments = sprintf("select * comments " . "xxx = %s order xxx desc", getsqlvaluestring(????????????, "text"));
well, if you're looking way list pages in order of comments, group page id , order count, like:
select page_id, count(*) comments group page_id order 2 desc, 1 asc
technically, don't need 1 asc
ensure specific order within descending comment count. so, if lot of pages identical comment count appear, can locate specific page within group easily. in other words, if page 7 had 2 comments , other pages had one, you'd (7,1,2,3,4,5,6,8,9)
. without 1 asc
, pages 1 through 6 , 8 through 9 come in order, (7,6,2,4,3,9,1,8,5)
, change between runs of query.
for example, build sample table:
> drop table comments; > create table comments (page_id integer,comment varchar(10)); > insert comments values (1,'1a'); > insert comments values (2,'2a'); > insert comments values (1,'1b'); > insert comments values (3,'3a'); > insert comments values (2,'2b'); > insert comments values (1,'1c'); > insert comments values (3,'3b'); > insert comments values (3,'3c'); > insert comments values (3,'3d');
then show data:
> select * comments order 1, 2; +---------+---------+ | page_id | comment | +---------+---------+ | 1 | 1a | | 1 | 1b | | 1 | 1c | | 2 | 2a | | 2 | 2b | | 3 | 3a | | 3 | 3b | | 3 | 3c | | 3 | 3d | +---------+---------+
then run select group descending comment count:
> select page_id,count(*) quant comments group page_id order 2 desc, 1 asc; +---------+-------+ | page_id | quant | +---------+-------+ | 3 | 4 | | 1 | 3 | | 2 | 2 | +---------+-------+
Comments
Post a Comment