-1
id first_die
24 2
25 6

How do i return the most recent first_die in this set? I want to display the latest roll in my select query. I know returning the latest first_die will be based on id but I'm having difficulty doing this because it is a simple fix, but I'm looking at not so simple answers to the problem. Any help would be nice. Thank you.

castaway13
  • 33
  • 4

2 Answers2

0

If your most recent id is the maximum number then you would use SELECT MAX(id) query (sql query).

If you want to return the entire recordset or row use the code below

SELECT * FROM tablename WHERE id=(SELECT MAX(id) FROM tablename);
  • No. Still getting an error message. This is my query syntax: $query = "SELECT first_die from first_roll WHERE MAX id"; – castaway13 Jan 16 '22 at 20:54
  • I added an example because i know you would be having problem using the MAX function the right way. Try the code example and let me know. – Ngutor Tsenongo Jan 16 '22 at 20:56
  • Still getting an error message. This is what follows my query: $result = mysqli_query($conn, $query); while($row = mysqli_fetch_assoc($result)) { $first_die = $row['first_die']; echo "
    first die is ".$first_die; }
    – castaway13 Jan 16 '22 at 20:59
  • What is the Error? Let me know what you are not doing right. Another thing is copy this query and run in MySQL database directly to verify if it works then you know the problem is not the query. – Ngutor Tsenongo Jan 16 '22 at 21:02
0

Use:

select id,first_die
from test_tbl 
order by id desc limit 1 ;

Demo:

Or:

select id,first_die
from test_tbl 
where id = (select max(id) from test_tbl);

Demo:

Ergest Basha
  • 7,870
  • 4
  • 8
  • 28