Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. only get one last record with the same field value during sorting database records

only get one last record with the same field value during sorting database records

Scheduled Pinned Locked Moved ASP.NET
databasealgorithmsquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Farhad Eft
    wrote on last edited by
    #1

    Hi, I have a table in database for messages and it has these fields: MessageId / SenderId / Message / Date I'd like to create a list of messages but only show the latest message from each user not listing whole messages just like how facebook shows messages. Is there a way to get the exact result by a sql query or I have to first get all the data from the database then build the list I want? Thank you

    W V 2 Replies Last reply
    0
    • F Farhad Eft

      Hi, I have a table in database for messages and it has these fields: MessageId / SenderId / Message / Date I'd like to create a list of messages but only show the latest message from each user not listing whole messages just like how facebook shows messages. Is there a way to get the exact result by a sql query or I have to first get all the data from the database then build the list I want? Thank you

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      You can do this (at least partially) in the db. For example:

      SELECT ...
      SUBSTRING(m.Message,1, 80) AS PartialMessage,
      ...
      FROM Messages m
      WHERE m.MessageID IN (SELECT MAX(m2.MessageId)
      FROM Messages m2
      GROUP BY m2.SenderId)

      The above (with typos corrected etc:)) should fetch the latest messsages for each sender and bring only first 80 characters of the message. In the example I'm relying on a fact that the MessageId field is constantly growing number so it will order the messages correctly. If this is not true then you would have to use the date field to get the latest messages.

      The need to optimize rises from a bad design.My articles[^]

      1 Reply Last reply
      0
      • F Farhad Eft

        Hi, I have a table in database for messages and it has these fields: MessageId / SenderId / Message / Date I'd like to create a list of messages but only show the latest message from each user not listing whole messages just like how facebook shows messages. Is there a way to get the exact result by a sql query or I have to first get all the data from the database then build the list I want? Thank you

        V Offline
        V Offline
        Viral Upadhyay
        wrote on last edited by
        #3

        Farhad Eft wrote:

        Is there a way to get the exact result by a sql query

        Yes if you want sorted data then use 'order by' statement. if you want some condition data then use 'where' condition. No need to get all the data from database and then build list.

        Viral My Blog Save Our Tigers

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups