Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MAX value in mysql query

Tags:

mysql

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 
like image 977
thersa Avatar asked Aug 11 '12 09:08

thersa


People also ask

How do you find the max in a query?

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.

How do I get the max value of a column in SQL?

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.

What is Max function in MySQL?

The MAX() function returns the maximum value in a set of values.

What is the syntax of Max () in SQL?

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.


2 Answers

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
like image 83
valex Avatar answered Oct 13 '22 20:10

valex


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

like image 34
Prince Jea Avatar answered Oct 13 '22 19:10

Prince Jea