Get MAX value in mysql query

前端 未结 3 1595
无人共我
无人共我 2021-01-05 05:38

I have a table

       id     mid    userid    remarks
       1       2       8          7 
       2       2       8          6
       3       2       8               


        
相关标签:
3条回答
  • 2021-01-05 06:05

    for mysql Node.js developers especially

    I was unable to get direct-value in mysql-driver for node.js i.e. when I run the following query

    SELECT MAX(id) FROM Users
    

    and I got the result as [RowDataPacket { MAX(id): 1 }]

    I tried to retrieve the value from object console.log(MAX(id)), but I couldn't

    so I set an alias i.e. maxid

    SELECT MAX(id) as maxid FROM Users
    

    and got it as [RowDataPacket { maxid: 1 }]

    Which I can retrieve as console.log(maxid)

    0 讨论(0)
  • 2021-01-05 06:06

    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

    0 讨论(0)
  • 2021-01-05 06:24
    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
    
    0 讨论(0)
提交回复
热议问题