简体   繁体   English

在MySQL栏中找到最大值

[英]Find Max Value in MySQL Column

seems like this question has been answered alot, but I can't get it to work! 似乎这个问题已经得到了很多回答,但我无法解决它!

Have a 'Month' column in DB table, that reaches 313 as max entry. 在数据库表中有一个“月”列,最大条目数达到313。

Am trying to assign this max amount to a PHP value which is working but keeps returning $maxmonth as 99? 是否正在尝试将此最大金额分配给可以正常工作但仍将$ maxmonth返回为99的PHP值?

Current Coding: 当前编码:

$result=mysqli_query($con,"SELECT MAX(Month) AS max from $ulliabforecast");

            {while ( $row = $result->fetch_assoc() )
            $maxmonth=$row['max'];}

Appreciate any help. 感谢任何帮助。

似乎您的MonthVARCHAR ,您需要执行CAST使其正常工作。

SELECT MAX(CAST(Month AS SIGNED)) AS max1 from $ulliabforecast

Try 尝试

SELECT `month` FROM $ulliabforecast ORDER BY `month` DESC LIMIT 0,1

As your sql query, this should select the largest month column from your database. 作为您的sql查询,这应从数据库中选择最大的月份列。

You might be using Month column as a VARCHAR type, change it to INT and your problem will be solved. 您可能将Month列用作VARCHAR类型,将其更改为INT解决问题。

Have a good day. 祝你有美好的一天。

You can be using month as VARCHAR . 您可以将month用作VARCHAR Please Check. 请检查。

And if true you can convert it to UNSIGNED and then get Max out of it 如果为true,则可以将其转换为UNSIGNED ,然后从中获得Max

Please try below SQL Statement 请尝试下面的SQL语句

SELECT MAX(CAST(`month` AS UNSIGNED)) AS max FROM $ulliabforecast 



Note 注意

As described in Cast Functions and Operators : 强制转换函数和运算符中所述

The type for the result can be one of the following values: 结果的类型可以是以下值之一:

  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM