ojsglobal
ojsglobal

Reputation: 534

SQL WHERE condition for the most recent row

I know this should be super easy, but I just can't get my head around it.

I have a table with values like this:

+----+-------+
| ID | VALUE |
+----+-------+
|  1 |   100 |
|  2 |   200 |
|  3 |   100 |
|  4 |   300 |
+----+-------+

I want to check if the most recent row (id 4) have the value 100.

I can't use LIMIT 1 or anything, and I'm sure I should use MAX() somehow, but I don't know where to start.

Thanks

Upvotes: 0

Views: 55

Answers (1)

Galma88
Galma88

Reputation: 2546

Try this:

SELECT * -- (or others fields you need)
FROM YOUR_TABLE AS A
WHERE A.ID = (SELECT MAX(B.ID)
              FROM YOUR_TABLE AS B) AND A.VALUE = 100 --(OR ELSE)

Upvotes: 2

Related Questions