2

just wondering if it is possible to get the top 10 COUNT results and ordering by COUNT and alphabetically?

I have the following tables,

tags
-------
id | title

.

tagged
------
tag_id | post_id

And the following SQL query

SELECT tag.*, COUNT(td.tag_ID) AS tagcount
FROM Tagged td
LEFT JOIN Tags tag ON td.tag_ID = tag.tag_ID
GROUP BY td.tag_ID
ORDER BY tagcount DESC, tag.tag_Title ASC

Any ideas?

Thanks in advance


Edit

Sorry if I didnt explain it properly.

The query works and I didnt add LIMIT 10 due to wanting to see the entire result set first.

The query I have works, however at the following example result

tag_ID  tag_Title  tagcount
1          Science  3
3          Chemistry 2
4          Misc      1
5          Maths       1
2          Sport       1

I would want Chemistry to come above Science though.

i.e. top ten highest counts.. sorted alphabetically


Result

Thanks to you both.. Daniel and Sled.

Here is a working example

( 
   SELECT     t.*, COUNT(*) AS tagcount
   FROM       tagged td
   LEFT JOIN  tags t ON (t.id = td.tag_id)
   GROUP BY   td.tag_id
   ORDER BY   tagcount DESC, t.title ASC
   LIMIT      3
) ORDER BY title ASC;

2 Answers 2

3

UPDATE:

Further to the new comment below:

( 
   SELECT     t.*, COUNT(*) AS tagcount
   FROM       tagged td
   LEFT JOIN  tags t ON (t.id = td.tag_id)
   GROUP BY   td.tag_id
   ORDER BY   tagcount DESC, t.title ASC
   LIMIT      3
) ORDER BY title ASC;

Result:

+------+------------+----------+
| id   | title      | tagcount |
+------+------------+----------+
|    3 | javascript |        2 |
|    1 | mysql      |        2 |
|    2 | php        |        3 |
+------+------------+----------+
3 rows in set (0.00 sec)

Simply change the LIMIT 3 to LIMIT 10 to get the top 10 instead of the top 3.


Previous Answer:

Why don't you add a LIMIT 10 to your query?

SELECT     t.*, COUNT(*) AS tagcount
FROM       tagged td
LEFT JOIN  tags t ON (t.id = td.tag_id)
GROUP BY   td.tag_id
ORDER BY   tagcount DESC, t.title ASC
LIMIT      10;

Test case:

CREATE TABLE tags (id int, title varchar(20));
CREATE TABLE tagged (tag_id int, post_id int);

INSERT INTO tags VALUES (1, 'mysql');
INSERT INTO tags VALUES (2, 'php');
INSERT INTO tags VALUES (3, 'javascript');
INSERT INTO tags VALUES (4, 'c');

INSERT INTO tagged VALUES (1, 1);
INSERT INTO tagged VALUES (2, 1);
INSERT INTO tagged VALUES (1, 2);
INSERT INTO tagged VALUES (2, 2);
INSERT INTO tagged VALUES (3, 3);
INSERT INTO tagged VALUES (2, 4);
INSERT INTO tagged VALUES (3, 4);
INSERT INTO tagged VALUES (4, 5);

Result (using LIMIT 3):

+------+------------+----------+
| id   | title      | tagcount |
+------+------------+----------+
|    2 | php        |        3 |
|    3 | javascript |        2 |
|    1 | mysql      |        2 |
+------+------------+----------+
3 rows in set (0.00 sec)

Note how the [c] tag fell out of the top 3 results, and rows are ordered alphabetically in case of a tie.

3
  • i would want PHP to come last though because its the alphabetically ASC of the top 3 results. If you know what I mean?
    – Stevanicus
    Sep 7, 2010 at 19:09
  • @Stevanicus: I think I understand now. Updated my answer with a new example. Is this what you intend? Sep 7, 2010 at 19:20
  • the only thing if mysql were to be 1, would it still appear in second place? Or would it drop down below PHP... it doesnt on mine.
    – Stevanicus
    Sep 7, 2010 at 19:33
2

Does the query work? If yes, you could use LIMIT 0, 10 to get only the top 10 rows.

SELECT tag.*, COUNT(td.tag_ID) AS tagcount
FROM Tagged td
LEFT JOIN gen_Tags tag ON td.tag_ID = tag.tag_ID
GROUP BY td.tag_ID
ORDER BY tagcount DESC, tag.tag_Title ASC LIMIT 0, 10

Another thing you might be interested in, is ranking. See here: http://www.fromdual.com/ranking-mysql-results

Edit

Maybe a subquery does what you want:

SELECT list.* FROM (
  SELECT tag.*, COUNT(td.tag_ID) AS tagcount, 
  FROM Tagged td
  LEFT JOIN gen_Tags tag ON td.tag_ID = tag.tag_ID
  GROUP BY td.tag_ID
  ORDER BY tagcount DESC LIMIT 0, 10
) AS list ORDER BY list.tag_Title ASC, list.tagcount DESC
5
  • yea its LIMIT gives me to top 10 rows, but im really looking for the 10 highest numbers, listed alphabetically
    – Stevanicus
    Sep 7, 2010 at 19:08
  • then you have to flip the order: ORDER BY tag.tag_Title ASC, tagcount DESC LIMIT 0, 10
    – sled
    Sep 7, 2010 at 19:15
  • yea thats what I thought but then i will get a result 2, 1, 1, 3... the titles may be ASC but I would want a result (count) 2,3,1,1
    – Stevanicus
    Sep 7, 2010 at 19:19
  • if i want the top 2 results. I would get tags with 2 and 1 counts. But really the tag with 3 is higher.
    – Stevanicus
    Sep 7, 2010 at 19:28
  • please check the new query in my answer.
    – sled
    Sep 7, 2010 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.