简体   繁体   English

在mysql查询中获取MAX值

[英]Get MAX value in mysql query

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. 我需要在该行之前的最后一行备注。 ie, remark '1' 即,评论'1'

SELECT MAX(id),mid,userid,remarks FROM sample 
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 SQL FIDDLE DEMO

for mysql Node.js developers especially 特别是对于mysql Node.js开发人员

I was unable to get direct-value in mysql-driver for node.js ie when I run the following query 我无法在node.js的mysql-driver中获得直接值,即当我运行以下查询时

SELECT MAX(id) FROM Users

and I got the result as [RowDataPacket { MAX(id): 1 }] 我的结果为[RowDataPacket { MAX(id): 1 }]

I tried to retrieve the value from object console.log(MAX(id)) , but I couldn't 我试图从对象console.log(MAX(id))检索值,但我不能

so I set an alias ie maxid 所以我设置了一个aliasmaxid

SELECT MAX(id) as maxid FROM Users

and got it as [RowDataPacket { maxid: 1 }] 得到它作为[RowDataPacket { maxid: 1 }]

Which I can retrieve as console.log(maxid) 哪个我可以检索为console.log(maxid)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM