少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          先建立表:
          CREATE TABLE `student` (                                  
                     `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',      
                     `name` varchar(100) DEFAULT NULL COMMENT 'name',        
                     `ban` varchar(100) DEFAULT NULL COMMENT 'ban',          
                     `score` int(11) DEFAULT NULL,                           
                     PRIMARY KEY (`id`),                                     
                     KEY `inx_ban` (`ban`)                                   
                   ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1  

          name:學生名
          ban:班級
          score:分數
          1、按班級分組排序,取出分數前兩名的同學。
          select t.ban,t.score,t.name from student t where 2<(select count(*) from student k where k.ban=t.ban and t.score>k.score order by k.ban desc) order by t.ban,t.score desc;
          示例如下:
          one 100 abin1
          one 99 abin2
          three 100 varyall1
          three 99 varyall2
          two 100 lee1
          two 99 lee2
          2、按組統計出來每組的所有分組,用逗號隔開
          select t.ban,group_concat(t.score) from student t group by t.ban
          示例如下:
          one 100,99,97,95,91
          three 100,99,97,95,91
          two 100,99,97,95,91



          posted on 2015-04-17 01:29 abin 閱讀(389) 評論(0)  編輯  收藏 所屬分類: mysql
          主站蜘蛛池模板: 重庆市| 丰原市| 丰顺县| 且末县| 玉屏| 类乌齐县| 邛崃市| 濉溪县| 鲁甸县| 东安县| 兰州市| 霍林郭勒市| 五大连池市| 洪江市| 宁明县| 塘沽区| 保定市| 七台河市| 东台市| 江都市| 吉木萨尔县| 青川县| 平舆县| 鄂伦春自治旗| 信丰县| 灵石县| 麦盖提县| 泾阳县| 娄底市| 庄河市| 喜德县| 偏关县| 彭山县| 安国市| 山东省| 老河口市| 鄂伦春自治旗| 微博| 准格尔旗| 赤壁市| 时尚|