1

i have table as following

Id - Number - value
1  - 1000   - 20
2  - 2000   - 30
3  - 3000   - 35

While I tried with

$sel = $mysqli->query("SELECT max(number) AS max_number, id FROM table ");
$sel_row = $sel->fetch_object();
$id = $sel_row->id;

I get $id = 1, but I am looking for $id = 3;

Tamil Selvan C
  • 18,342
  • 12
  • 44
  • 63
vajra
  • 21
  • 1
  • 10
  • Your query means: "select maximum `number` column and some random `id` column". So the result is pretty expected. – zerkms Mar 25 '14 at 02:47
  • Dup of: http://stackoverflow.com/questions/2081211/mysql-select-maxscore-doesnt-return-the-relevant-row-data-how-to-solve-the-i and http://stackoverflow.com/questions/7604893/sql-select-row-from-table-where-id-maxid – Aziz Saleh Mar 25 '14 at 02:52
  • 2
    You can rework your query as: `SELECT number, id FROM table ORDER BY number DESC LIMIT 1` – Aziz Saleh Mar 25 '14 at 02:54
  • use `SELECT number AS max_number, id FROM table ORDER BY number DESC LIMIT 1` – Tamil Selvan C Mar 25 '14 at 02:55
  • Possible duplicate of [How do I select an entire row which has the largest ID in the table?](https://stackoverflow.com/questions/7604893/how-do-i-select-an-entire-row-which-has-the-largest-id-in-the-table) – Dharman Jul 19 '19 at 20:15

0 Answers0