MySQL Forums
Forum List  »  General

Re: basic join question
Posted by: Rick James
Date: May 20, 2012 01:57PM

# The three most populous cities in each state:
CREATE TEMPORARY TABLE t (
    seq SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (state, seq)
)  ENGINE = MyISAM   -- required for the PRIMARY KEY trick
SELECT state, city, population
    FROM US
    ORDER BY state, population DESC;
SELECT state, seq, city, population
    FROM t
    WHERE seq <= 3
    ORDER BY state, seq;
+-------+-----+--------------+------------+
| state | seq | city         | population |
+-------+-----+--------------+------------+
| AK    |   1 | Anchorage    |     276263 |
| AK    |   2 | Juneau       |      31796 |
| AK    |   3 | Fairbanks    |      31351 |
| AL    |   1 | Birmingham   |     231621 |
| AL    |   2 | Montgomery   |     198325 |
| AL    |   3 | Mobile       |     190274 |
| AR    |   1 | Little Rock  |     184217 |
| AR    |   2 | Fort Smith   |      81985 |
| AR    |   3 | Fayetteville |      64864 |
| AZ    |   1 | Phoenix      |    1428509 |
| AZ    |   2 | Tucson       |     518907 |
...

Options: ReplyQuote


Subject
Written By
Posted
May 19, 2012 04:00PM
Re: basic join question
May 20, 2012 01:57PM
May 20, 2012 06:09PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.