简体   繁体   English

如何根据学生的分数来使其排名(第,第2,第3,第4)

[英]How to make Students Position (ist, 2nd, 3rd, 4th) depending on their scores

            CREATE TABLE IF NOT EXISTS `users` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `names` varchar(30) NOT NULL,
            `matricno` varchar(30) NOT NULL,
            `class` varchar(30) NOT NULL,
            `scores` varchar(30) NOT NULL,
            `subject` varchar(30) NOT NULL,
        PRIMARY KEY (`id`)
        )   ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;




        $con = mysqli_connect("localhost", "root", "1024", "sch_one");
            if (!$con) {
                echo "Failed to connect to Database". mysqli_error();
        }



        INSERT INTO `users` (`id`, `names`, `matricno`, `class`, `scores`) VALUES
        (1, 'Henry Okon Etim', '15/HCSS/001', 'A', 800),
        (2, 'Henry Okon Etim', '15/HCSS/002', 'A',  804),
        (3, 'Etim Okon Etim', '15/HCSS/003', 'A',  820),
        (4, 'Lavalish Okon Etim', '15/HCSS/004',  'A', 730),
        (5, 'Pherema Okon Etim', '15/HCSS/005',  'A', 920),
        (6, 'Leman Okon Etim', '15/HCSS/006', 'A', 803),
        (7, 'Lema3 Okon Etim', '15/HCSS/007',  'A', 500);

I am on a project that gives students positions based on their final scores. 我参与的一个项目根据学生的最终成绩为他们提供职位。 My problem is how to output positions according to the scores in a as 1st, 2nd, 3rd, 4th, 5th and so on. 我的问题是如何根据1、2、3、4、5等中的分数输出位置。 Please explanations and codes are welcome. 请解释和欢迎代码。 Thanks! 谢谢!

请尝试以下代码:

SELECT * FROM users ORDER BY scores DESC;

From what I'm understanding you want to pimary keys(1/2/3..) to reflect their position? 根据我的理解,您想使用键(1/2/3 ..)来反映它们的位置吗?

That's not what the id's are for, if you want to order them you can just add an order by in your get query. 这不是ID的目的,如果要订购它们,只需在get查询中添加一个订购者即可。

$result = $mysqli->query("SELECT * FROM users ORDER BY scores desc");

If you really want an index/counter in your select, you could always take a look at ranking in mysql, end result would be something like this: 如果您确实想要选择索引/计数器,则可以随时查看mysql中的排名,最终结果将如下所示:

SET @rank=0;
SELECT @rank:=@rank+1 AS rank, users.* FROM users ORDER BY scores DESC;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在php中显示当前星期的1、2、3、4和5日 - Display the 1st, 2nd, 3rd, 4th, & 5th date of the current week in php PHP日期和时间,在一个月内获得第1周,第2周,第3周和第4周 - PHP Date and Time, getting the 1st, 2nd, 3rd, and 4th week in a month 如何在选项标签 select 下拉列表 1st 2nd 3rd 4th 5th 中添加上标。 1ST 我想要 S 大写字母,但我试过 s 将转换为小写 - How to add superscript in option tag select dropdown 1st 2nd 3rd 4th 5th. 1ST I want S captial letters but i tried s will converted to small case 如何对数组中的每个第二,第三和第五个元素进行运算? - How to operate on every 2nd, 3rd and 5th element in an array? 在序号字符串(第二,第八,第三,第一等)上分割地址字符串 - Splitting address strings on Sequential-Number strings (2nd, 8th, 3rd, first, etc..) 如何使用while(!feof)仅选择一行的第2个和第4个值? - How to pick only every 2nd and 4th value of a line using while(!feof)? 如何使用多维数组中的第 4 级值对第 3 级子数组进行排序? - How to sort 3rd level subarrays using 4th level values in a multi-dimensional array? 我想在考试成绩中显示第 1、2、3 位 - I want to show position 1st, 2nd, 3rd in exam result 从php中的数组中删除第3个和第4个元素 - Remove every 3rd and 4th element from array in php 我试图在一个新闻框中显示第3、4、6、7等wordpress博客文章。 我所附的图片解释得更好 - I am trying to make the 3rd, 4th, 6th, 7th etc wordpress blog posts display in one news box. The pictures i have attached explains better
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM