简体   繁体   中英

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.

Am trying to assign this max amount to a PHP value which is working but keeps returning $maxmonth as 99?

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.

You might be using Month column as a VARCHAR type, change it to INT and your problem will be solved.

Have a good day.

You can be using month as VARCHAR . Please Check.

And if true you can convert it to UNSIGNED and then get Max out of it

Please try below SQL Statement

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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