当值为十进制和非十进制时,如何使用子查询从 mysql table 获取最大列值

how to get the max column value from msyql table using subquery when the values are decimal and non decimal

+----+-------------------------+--------+------------+-------+--------+--------------+------------+---------+-----------+
| id | user_email              | cat_id | sub_cat_id | score | out_of | score_in_per | date       | groupId | groupType |
+----+-------------------------+--------+------------+-------+--------+--------------+------------+---------+-----------+
| 13 | inststudent@yopmail.com | 9      | 11         | 40    | 40     | 100          | 22-04-2017 | 34      | institute |
+----+-------------------------+--------+------------+-------+--------+--------------+------------+---------+-----------+
| 14 | inststudent@yopmail.com | 9      | 11         | 37    | 40     | 92.5         | 22-04-2017 | 34      | institute |
+----+-------------------------+--------+------------+-------+--------+--------------+------------+---------+-----------+
| 15 | inststudent@yopmail.com | 9      | 11         | 35    | 54     | 90.35        | 22-04-2017 | 34      | institute |
+----+-------------------------+--------+------------+-------+--------+--------------+------------+---------+-----------+
| 17 | inststudent@yopmail.com | 9      | 11         | 75    | 41     | 91.52        | 22-04-2017 | 34      | institute |
+----+-------------------------+--------+------------+-------+--------+--------------+------------+---------+-----------+
| 20 | inststudent@yopmail.com | 9      | 11         | 47    | 56     | 85           | 22-04-2017 | 34      | institute |
+----+-------------------------+--------+------------+-------+--------+--------------+------------+---------+-----------+
| 35 | inststudent@yopmail.com | 9      | 11         | 14    | 89     | 20.45        | 22-04-2017 | 34      | institute |
+----+-------------------------+--------+------------+-------+--------+--------------+------------+---------+-----------+
| 37 | inststudent@yopmail.com | 9      | 11         | 69    | 78     | 45.45        | 22-04-2017 | 34      | institute |
+----+-------------------------+--------+------------+-------+--------+--------------+------------+---------+-----------+

从上面 table 我想获得最大 score_in_per 值作为子查询但是由于十进制它不能通过 desc 或 max(score_in_per) 工作所以你怎么能得到最大值 score_in_per

this is what i have tried but the max value is not coming..

(SELECT MAX(score_in_per) 
FROM tbl_student_skill_score WHERE cat_id=9 
and sub_cat_id=11
ORDER BY score_in_per DESC)  as maxPortaScore 

这里有两种方法。如果你想使用MAX()功能,那么你不需要ORDER BY。因此,以下应该有效:

SELECT MAX(CAST(score_in_per AS DECIMAL(10,2))) AS maxPortaScore
FROM tbl_student_skill_score
WHERE cat_id = 9 AND sub_cat_id = 11

另一种方法是在分数列上使用 ORDER BY,然后将结果集限制为仅第一条记录,即最大分数:

SELECT CAST(score_in_per AS DECIMAL(10,2))
FROM tbl_student_skill_score
WHERE cat_id = 9 AND sub_cat_id = 11
ORDER BY CAST(score_in_per AS DECIMAL(10,2)) DESC
LIMIT 1

我根据接受的答案更新了上面的内容,这意味着 score_in_per 被存储为文本。

SELECT  
max(cast(score_in_per as decimal(5,2)))
FROM tbl_student_skill_score WHERE cat_id=9 
and sub_cat_id=11

试试上面的代码。 希望对你有帮助。