F Q F Q - 2 months ago 6
SQL Question

mysql querry for specific record

I have a mysql table which contains record of different status of books. A same book may have multiple records but each record have same unique id i.e bid like this

id bookname bid
1 vb.net 681
2 html 682
3 vb.net 681
4 vb.net 681
5 html 682


I want to pick the last record of specific bid 681 or 682. Please help me to make sql query for this.

Answer

Have a sub-query that returns each bid's max id. Join with that result:

select t1.*
from tablename t1
join (select max(id) as maxid, bid
      from tablename
      group by bid) t2 on t1.id = t2.maxid
Comments