列中的最大值

时间:2016-03-12 04:27:42

标签: php mysql max

有更有效的方法吗?我希望使用PHP在给定的MySQL表中获得列(int)的最大值。

$results = $conn->query("SELECT statement_id FROM statement ORDER BY statement_id DESC LIMIT 1");
$next_statment_id = $results->fetch_assoc();
$next_statment_id = $next_statment_id['statement_id'];
echo $next_statment_id;

2 个答案:

答案 0 :(得分:2)

MAX也是一个好的方法

SELECT MAX(statement_id) AS statementIdentity 
  FROM statement;

输出产生如下: -

+-------------------+
| statementIdentity |
+-------------------+
|       4           |
+-------------------+

答案 1 :(得分:1)

请考虑以下事项:

SELECT COUNT(*) FROM test;
+----------+
| COUNT(*) |
+----------+
| 10000000 |
+----------+

SELECT i FROM test ORDER BY i DESC LIMIT 1;
+----------+
| i        |
+----------+
| 18482903 |
+----------+
1 row in set (0.00 sec)

SELECT MAX(i) FROM test;
+----------+
| MAX(i)   |
+----------+
| 18482903 |
+----------+
1 row in set (0.00 sec)

请参阅。其中并不多。