mysql排序和排名声明 [英] mysql sorting and ranking statement

查看:89
本文介绍了mysql排序和排名声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mysql语句中需要一些帮助 我有7列的table1和有8列的表2的额外列名为ranking,我的语句应该像 从表1中选择全部,然后按用户数"对其进行排序,将其插入表2中,并从1 2 3开始排名,等等,

I need some help in mysql statement Ive table1 with 7 column and table 2 with 8 column the extra column named ranking , my statement should be like select all from table 1 then sort it by " number of users " insert it in table 2 and ranking start 1 2 3 etc,

table 1 : 
username |     email         | number of users
jack          a@a.com               75
ralf          b@b.com               200
anne          c@c.com                12
sonny         d@d.com                300

==================================

===================================

在这里我需要根据用户数量进行插入和排名

here where i need to INSERT and RANKING based on number of users

table 2 

ranking    | username |     email         | number of users
1
2
3

推荐答案

INSERT INTO table2
  SELECT @rank := @rank + 1, table1.* FROM table1
  JOIN( SELECT @rank := 0 ) AS init
  ORDER BY number_of_users DESC

这篇关于mysql排序和排名声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆