phpmysqlcodeignitermaxsql-order-by

Fetch record with MAX() column value in CodeIgniter


I want to echo the record that has the MAX() value in a specified column using CodeIgniter. How do I do that?

I have table like this:

id|value
1 | 3
2 | 1

What I've tried so far is to use query: SELECT MAX(value) FROM user

but it just echo max user,so how can I show the record with id that has MAX() value?


Solution

  • If you need only one row with actual data you should use another appoach with sorting and limit. See code below:

    SELECT * FROM user order by value desc limit 1;