-1

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      |

Code results: 9, the question is why? Mysql "something" type is "text". My mysql table is bigger than this above, but it still results not the biggest "something".

099
  • 333
  • 1
  • 15
Arnas Marnas
  • 43
  • 1
  • 1
  • 6

1 Answers1

1

Either do a math operation on the column to trigger an auto-cast

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

or cast explicitly with

SELECT MAX(cast(something as signed)) AS something FROM users

but even better - if that column only contains numbers then change the data type of that field ti int for instance.

juergen d
  • 201,996
  • 37
  • 293
  • 362
  • care to explain this? I'm having a danish myself to satisfy my hungry tummy ;-) – Funk Forty Niner Aug 26 '15 at 13:45
  • why not set the columns to `INT` instead? – CodeGodie Aug 26 '15 at 13:45
  • @CodeGodie I'm wondering that myself and why OP is using integers to start with. – Funk Forty Niner Aug 26 '15 at 13:49
  • 1
    Even if there's a reason why OP needs them as `TEXT`, it is just bad design from my perspective. – CodeGodie Aug 26 '15 at 13:50
  • 2
    @ArnasMarnas As stated; it's bad design. This will mean that you will have to cast everytime you want to perform a mathematical operation, using MySQL's other aggregate functions. If you're in an early stage, best to rethink this. *My 2 cents of course*, which may be worth a pound of gold later on. Having to "cast", means more code for what's needed, including resources. – Funk Forty Niner Aug 26 '15 at 13:57