Get the Largest Number in a mySQL Database in PHP

2020-03-01 16:09发布

I have a SQL Database that has a column like this:

ID
-----
0352
5432
4382
3520
30593
3992
295

What I want to do is search through that column, find the largest number (30593) and store it in a variable.

This database has other columns, the example above is for demonstration only.

e.g.

$largestNumber = GET LARGEST NUMBER FROM ID

How would I do that in PHP / MySQL

标签: php mysql
6条回答
霸刀☆藐视天下
2楼-- · 2020-03-01 16:12

SELECT MAX(ID) FROM TABLE

Execute the statement and assign it to your variable.

查看更多
做个烂人
3楼-- · 2020-03-01 16:14

Try This Query

 "SELECT MAX(Price) AS HighestPrice FROM Products";
查看更多
小情绪 Triste *
4楼-- · 2020-03-01 16:22

I believe SELECT max(id) FROM table will work.

查看更多
戒情不戒烟
5楼-- · 2020-03-01 16:27

Use MAX(ID) to get the largest value

查看更多
够拽才男人
6楼-- · 2020-03-01 16:37

In PHP, we do it like this:

$rowSQL = mysql_query( "SELECT MAX( ID ) AS max FROM `tableName`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];
查看更多
贼婆χ
7楼-- · 2020-03-01 16:38

You can do a single query to figure this out:

http://www.tutorialspoint.com/mysql/mysql-max-function.htm

MySql has it's own Max function that will return the highest value in the specified column.

查看更多
登录 后发表回答