I have a table
id mid userid remarks
1 2 8 7
2 2 8 6
3 2 8 4
4 2 8 5
5 2 8 2
6 2 8 3
7 2 8 7
8 2 8 0
9 2 8 1
10 2 8 8
I need the last row of remark before that row. i.e., remark '1'
SELECT MAX(id),mid,userid,remarks FROM sample
SQL MIN() and MAX() Functions The MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.
To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.
The MAX() function returns the maximum value in a set of values.
SELECT MAX(salary) AS "Highest salary" FROM employees; In this SQL MAX function example, we've aliased the MAX(salary) field as "Highest salary". As a result, "Highest salary" will display as the field name when the result set is returned.
Select Id,mid,userid,remarks from sample Where id<(select max(Id) from sample)
order by id desc limit 1
Or
Select Id,mid,userid,remarks from sample
order by id desc limit 1 offset 1
Try this:
SELECT MAX(id),mid,userid,remarks
FROM sample WHERE id NOT IN (
SELECT MAX(id) FROM sample
)
GROUP BY mid,userid,remarks
EDIT
See if this works
SQL FIDDLE DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With