按国家/地区代码

时间:2015-04-23 17:59:53

标签: mysql sql

我在这里有一个查询,我希望按国家/地区计算最多的国家/地区。但是,它给我一个错误,如果有人可以帮助它将非常感激!

以下是查询!

SELECT country_code,count(country_code) FROM `users` 
GROUP BY country_code
ORDER BY `users`.`id`
DESC LIMIT 50;

所以它就像

|美国| 150 |

| CA | 200 |

表格结构

    CREATE TABLE IF NOT EXISTS `users` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `username` varchar(50) NOT NULL,
      `password` varchar(50) NOT NULL,
      `real_name` varchar(50) NOT NULL,
      `mail` varchar(50) NOT NULL DEFAULT 'defaultuser@other.org',
      `rank` int(11) unsigned NOT NULL DEFAULT '1',
      `motto` varchar(255) NOT NULL DEFAULT 'I love Fatal Hotel!',
      `account_created` varchar(50) NOT NULL,
      `last_online` int(15) NOT NULL,
      `online` enum('0','1') NOT NULL DEFAULT '0',
      `ip_last` varchar(120) NOT NULL,
      `ip_reg` varchar(120) NOT NULL,
      `home_room` int(10) unsigned NOT NULL DEFAULT '0',
      `daily_respect_points` int(11) NOT NULL DEFAULT '3',
      `daily_pet_respect_points` int(11) NOT NULL DEFAULT '3',
      `newbie_status` int(11) NOT NULL DEFAULT '0',
      `is_muted` enum('0','1') NOT NULL DEFAULT '0',
      `mutant_penalty` enum('0','1','2') NOT NULL DEFAULT '0',
      `mutant_penalty_expire` int(11) NOT NULL DEFAULT '0',
      `block_newfriends` enum('0','1') NOT NULL DEFAULT '0',
      `hide_online` enum('0','1') NOT NULL DEFAULT '0',
      `hide_inroom` enum('0','1') NOT NULL DEFAULT '0',
      `vip` enum('0','1') NOT NULL DEFAULT '0',
      `logged` int(11) NOT NULL,
      `points` int(11) NOT NULL,
      `country_code` varchar(20) NOT NULL,
      `profile_status` enum('0','1','2') NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`) USING BTREE
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED AUTO_INCREMENT=467835 ;

谢谢, 约什

2 个答案:

答案 0 :(得分:0)

这可能有效:

SELECT country_code,count(country_code) as total FROM `users` 
GROUP BY country_code
ORDER BY total
DESC LIMIT 50;

答案 1 :(得分:0)

你走了:

SELECT * FROM (SELECT country_code,count(country_code) AS 'Total' FROM `users` 
GROUP BY country_code
ORDER BY 'Total' DESC) t LIMIT 50;

SQL Fiddle example