Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query - Top 20 results in ascending alphabetical order 1

Status
Not open for further replies.

Nematoth

Programmer
Mar 29, 2002
48
MY
I have a table which is a list of categories in an online directory.. each time the category is visited in increases the hits column by one..

I want to get the top 20 results (ie. the top twenty most popular categories) and then sort them into a list alphabetically.

I tihnk I have to use a nested select but when I do this I only get the top 20 results sorted by alphabet not by hits..

Here is the table structure:

Code:
#
# Table structure for table `category`
#

CREATE TABLE category (
  autoID mediumint(9) NOT NULL auto_increment,
  title varchar(100) NOT NULL default '',
  hits int(11) NOT NULL default '0',
  PRIMARY KEY  (autoID)
) TYPE=MyISAM;

Can anyone help with this?

TIA.

N.

Nematoth
 
Code:
select title,hits
 from 
  (select title,hits
     from category
    order by hits desc
    limit 20) dt
order by title
 
I am running version MySQL 3.23.58

Could this be the problem? I saw another post somewhere stating that nested select only works on version 4.1 and above..

Is there a workaround?

Nematoth
 
Use a temp table?

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
You would have to use a temporary table.
[tt]
CREATE TEMPORARY TABLE t AS
SELECT * FROM category ORDER BY hits DESC LIMIT 20;
SELECT * FROM t ORDER BY title;

...

DROP TABLE t;
[/tt]
 
Great! Thats the way! Thanks John.

This is what I did:

Code:
CREATE TABLE new_tbl SELECT title, hits FROM category ORDER BY hits DESC LIMIT 20

SELECT title,hits FROM new_tbl ORDER BY title ASC

DROP TABLE new_tbl

Nematoth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top