get the row with the highest value in MySQL

后端 未结 3 623
后悔当初
后悔当初 2020-12-12 06:33

I want to get the highest value but group by another field on the same table ex:

seqid + fileid + name

1  |    1 | n1
2  |    1 | n2
3  |    2 | n3
         


        
相关标签:
3条回答
  • 2020-12-12 06:56

    Describe exactly what you need, provide bigger table, and result table, also I dont get it what you mean at all highest value but group by another field, highest value of what column?

    in your result you mention 4|3|n5, why not 5|3|n5 if you say Highest ?

    ok so you edited it..

    looks fairly simple something like

         select seqid, filed,name from table t1 join 
    (select max(seqid) seqid from table group by fileid) as t2 on t2.seqid=t1.seqid
    

    if seqid is PK you dont need anything but PK i dont get why would you want to join on 2 fields instead of just PK

    0 讨论(0)
  • 2020-12-12 06:59

    How about something like

    SELECT  t.*
    FROM    Table t INNER JOIN
            (
                SELECT  fileid,
                        MAX(seqid) Maxseqid
                FROM    Table
                GROUP BY    fileid
            ) m ON  t.fileid = m.fileid
                AND t.seqid = m.Maxseqid
    
    0 讨论(0)
  • 2020-12-12 07:09
    SELECT seqid, fileid, name
    FROM tbl JOIN (
        SELECT MAX(seqid) maxSeq, fileid fileid
        FROM tbl
        GROUP BY fileid
        ) tg ON tml.seqid = tg.maxSeq AND tbl.fileid = tg.fileid
    
    0 讨论(0)
提交回复
热议问题