用 PHP 获取 mySQL 数据库中的最大数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9798188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the Largest Number in a mySQL Database in PHP
提问by Talon
I have a SQL Database that has a column like this:
我有一个 SQL 数据库,其中有一列如下:
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.
我想要做的是搜索该列,找到最大的数字(30593)并将其存储在一个变量中。
This database has other columns, the example above is for demonstration only.
这个数据库还有其他的列,上面的例子只是为了演示。
e.g.
例如
$largestNumber = GET LARGEST NUMBER FROM ID
$largestNumber = 从 ID 获取最大数字
How would I do that in PHP / MySQL
我将如何在 PHP/MySQL 中做到这一点
回答by hjpotter92
In PHP, we do it like this:
在 PHP 中,我们这样做:
$rowSQL = mysql_query( "SELECT MAX( ID ) AS max FROM `tableName`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];
回答by qitch
I believe SELECT max(id) FROM table
will work.
我相信SELECT max(id) FROM table
会奏效。
回答by hkf
SELECT MAX(ID) FROM TABLE
SELECT MAX(ID) FROM TABLE
Execute the statement and assign it to your variable.
执行该语句并将其分配给您的变量。
回答by Dipesh
Try This Query
试试这个查询
"SELECT MAX(Price) AS HighestPrice FROM Products";
回答by xthexder
You can do a single query to figure this out:
您可以执行单个查询来解决这个问题:
http://www.tutorialspoint.com/mysql/mysql-max-function.htm
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.
MySql 有它自己的 Max 函数,它将返回指定列中的最高值。
回答by Anand
Use MAX(ID)
to get the largest value
使用MAX(ID)
来获取最大价值