SQL根据select分配唯一标识符

时间:2013-10-19 15:22:42

标签: mysql sql

我有一张有100张车记录的牌桌。我正在创建一个表单,其中选择的下拉列表选项将它们分为4或5辆汽车的组(由下拉列表选择选择),然后根据他们的年份降序和制作。在此之后,组将需要具有组ID(例如NIS01)。前3个字母是汽车的品牌,数字也是分配给它的组。

您如何为每个组排序和分配组ID?最好是ORDER BY还是GROUP BY然后创建一个函数来分配这个id?

我感谢任何帮助。提前致谢

编辑 - 抱歉应该在开头发布

创建TABLE汽车 ( CarID int(10), make varchar(255), 模型varchar(255), 年varchar(4), EngineSize varchar(8), );

示例数据


  CarID    ,   Make   ,   Model   ,   Year   ,   EngineSize   
  16528454,   Toyota,     Prius,      2007          1.8
  45619810,   Toyota,     AE-86,      2012,         2.0
  32541811,   Nissan,     Skyline,    2008,         3.0
  25856181,   Mitsubishi, Nimbus,     2000,         2.4
  29519815,   Toyota,     Celica,     2007,         1.8
  16916598,   Holden,     Barina,     2011,         1.6
  65619818,   Mitsubishi, Evolution,  2008,         2.4
  15616518,   Holden,     Commoodore, 2011,         6.0

组数据(基于4组)


  GroupID   ,   Make   ,   Model   ,   Year   ,   EngineSize   
  TOY101,     Toyota,     Prius,        2013,         1.8
  TOY101,     Toyota,     AE-86,        2013,         2.0
  TOY101,     Toyota,     Celica,       2013,         1.8  
  TOY101,     Toyota,     Prius,        2012,         1.8
  TOY102,     Toyota,     AE-86,        2012,         2.0  
  TOY102,     Toyota,     Celica,       2012,         1.8
  NIS201,     Nissan,     Skyline,      2013,         3.0
  NIS201,     Nissan,     Skyline,      2013,         3.0
  NIS201,     Nissan,     Silvia,       2013,         2.0
  NIS201,     Nissan,     Skyline,      2013,         3.0
  NIS202,     Nissan,     Pulsar,       2013,         2.0
  NIS202,     Nissan,     Skyline,      2012,         3.0
  NIS202,     Nissan,     Pulsar,       2009,         2.0
  NIS202,     Nissan,     Skyline,      2008,         3.0
  MIT301,     Mitsubishi, Nimbus,       2012,         2.4
  MIT301,     Mitsubishi, Evolution,    2013,         2.4
  MIT301,     Mitsubishi, Nimbus,       2012,         2.4
  MIT301,     Mitsubishi, Evolution,    2008,         2.4
  HOL401,     Holden,     Barina,       2013,         1.6
  HOL401,     Holden,     Commoodore,   2012,         6.0
  HOL401,     Holden,     Barina,       2011,         1.6
  HOL401,     Holden,     Commoodore,   2011,         6.0
  HOL402,     Holden,     Barina,       2010,         1.6
  HOL402,     Holden,     Commoodore,   2007,         6.0

0 个答案:

没有答案