PHP Mysql MAX()返回错误的结果

时间:2015-08-26 13:40:18

标签: php mysql

PHP:

$result = mysql_query("SELECT MAX(something) AS something FROM users");
$row = mysql_fetch_assoc($result);
$max =  $row["something"];
echo $max;

MySQL的:

+-----------+----------+
| Something | Name     |
+-----------+----------+
| 9         | John     |
| 984       | Somebody |
| 1         | Who      |

代码结果:9,问题是为什么? Mysql"东西" type是" text"。我的mysql表比上面的大,但它仍然不会产生最大的"东西"。

1 个答案:

答案 0 :(得分:1)

对列进行数学运算以触发自动转换

SELECT MAX(something * 1) AS something FROM users#

或使用

明确转换
SELECT MAX(cast(something as signed)) AS something FROM users

但更好 - 如果该列仅包含数字,则更改该字段ti int的数据类型。