Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4111216
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:02:16+00:00 2026-05-20T22:02:16+00:00

I am building a messaging system for a CMS and can not find a

  • 0

I am building a messaging system for a CMS and can not find a way to get the number of unread messages in a message chain.

I need a way to find out if the main message or any of it’s reply messages are unread.

I have a table called “messages” that has the fields:
id, active[1,0], subject, message, datetime, user_from, user_to, reply, viewed, removed

When a message is read I store that users id comma-delimited in the viewed field:
,3,4,12,

Then to find out if it’s viewed I look for the sender’s id like this:

m.viewed LIKE '%,$user_id,%'

I just figure out how to get that logic into this SQL select so that I can see if a parent message should be marked as unread. The following SQL works great and grabs all the messages and the number of replies and orders them by datetime.

SELECT m.*, COUNT(*) AS num_replies, MAX(r.datetime) AS max_datetimeunread_replies
FROM directus_messages AS m
LEFT JOIN directus_messages as r
    ON m.id = r.reply
WHERE m.active = '1' 
AND m.removed NOT LIKE '%,$user_id,%' 
GROUP BY m.id
HAVING m.reply = '0' 
ORDER BY datetime DESC, max_datetime DESC

Any help would be greatly appreciated… I can’t wrap my head around it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:02:17+00:00Added an answer on May 20, 2026 at 10:02 pm

    You should check this article: Managing Hierarchical Data in MySQL

    –EDIT–UPDATE

    OK, you only have 1 level of replies so no need for the above.

    Try this:

    SELECT m.*
         , COUNT(*) AS num_replies
         , MAX(r.datetime) AS max_datetime
         , (m.viewed LIKE '%,$user_id,%') 
             AS message_viewed    --shows True or False
         , SUM(r.viewed NOT LIKE '%,$user_id,%')
             AS unread_replies    --shows number of unread replies
    FROM directus_messages AS m
      LEFT JOIN directus_messages as r
        ON m.id = r.reply
    WHERE m.active = '1' 
      AND m.removed NOT LIKE '%,$user_id,%' 
    GROUP BY m.id
    HAVING m.reply = '0' 
    ORDER BY m.datetime DESC
           , max_datetime DESC
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok here is the deal i am building a messaging system by php,ajax,jquery and
I'm building a small messaging system like you have in facebook. Only thing is
Building a client-side swing application what should be notified on a bus (application-wide message
When building a VS 2008 solution with 19 projects I sometimes get: The GenerateResource
Im building a CMS (ASP.NET C#) and I am currently looking at how to
I am putting together a messaging system for a rails app I am working
I'm using Intridea's Acts as Readable Rails plugin for a messaging system I'm currently
Want to build an web based Instant Messaging system similar to www.chatzy.com The question
I'm building an SMS call and response system in a new app that receives
I'm building an asp.net MVC application where users can attach a picture to their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.