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

  • SEARCH
  • Home
  • 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 6881351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:05:48+00:00 2026-05-27T05:05:48+00:00

CREATE TABLE `messages` ( `message_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `message_project_id` int(7) unsigned NOT

  • 0
CREATE TABLE `messages` (
  `message_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `message_project_id` int(7) unsigned NOT NULL DEFAULT '0',
  `message_time` int(11) unsigned NOT NULL DEFAULT '0',
  `message_from_user_id` int(7) unsigned NOT NULL DEFAULT '0',
  `message_to_user_id` int(7) unsigned NOT NULL DEFAULT '0',
  `message_details` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`message_id`)
)


CREATE TABLE `project` (
  `project_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `project_user_id` int(7) unsigned NOT NULL DEFAULT '0',
  `project_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `project_status` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
 PRIMARY KEY (`project_id`)
) 

What im looking to retreive is the latest messages to user #2 for each project.

User #2 is the owner of the project so can receive messages from many interested parties.

The actual page will display a list of things “To Do” for user #2. I want to find any messages for current open projects in which user #2 has received a message, but not yet sent one

so if user #1 sent a message to user #2 there would be a row in the messages table

message_id | project_id | message_time | message_from_user_id | message_to_user_id | message_details
30 | 12 | 1304707966 | 1 | 2 | Hello user number two, thank you for your interest in my project
31 | 12 | 1304707970 | 2 | 1 | Hello user number one, Your project looks interesting
32 | 12 | 1304707975 | 3 | 1 | Hello user number one, here is my first message, im user number three.  I want to do your project
32 | 13 | 1304707975 | 7 | 1 | Hello user number one, here is my first message, im user number seven.  I want to do your other project

What I’ve tried so far but dont quite work:
//this will get me the most current message for each project but not separate by user

SELECT cur.*, p.*
FROM messages cur
LEFT JOIN messages next ON cur.message_project_id = next.message_project_id AND cur.message_time < next.message_time
LEFT JOIN project p ON p.project_id = cur.message_project_id
WHERE next.message_time IS NULL
AND (cur.message_from_user_id = 2 OR cur.message_to_user_id = 2)
AND (p.project_status LIKE 'open' OR p.project_status LIKE 'started')
AND p.project_user_id = 2
ORDER BY cur.message_time DESC 

//This will separate by user, but not return the most recent message text

SELECT *
FROM messages m
LEFT JOIN project p ON p.project_id = m.message_project_id
WHERE (message_from_user_id = 2 OR message_to_user_id = 2 )
AND (p.project_status LIKE 'open' OR p.project_status LIKE 'started')
AND p.project_user_id = 2
GROUP BY project_id
ORDER BY message_time DESC 

Once the data gets back to Php i check to see if the most recent message is TO user #2 and if it is then post a “You need to reply” message to his screen.

  • 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-27T05:05:48+00:00Added an answer on May 27, 2026 at 5:05 am

    It would have probably helped if you had given some example output that covers most of your cases. Judging from your comments and after rereading a couple of times though, I think this is what you’re looking for:

    SELECT
        <column list>
    FROM
        Messages M
    INNER JOIN Projects P ON
        P.project_id = M.message_project_id AND
        P.project_status IN ('open', 'started') AND
        P.project_user_id = 2
    WHERE
        M.message_to_user_id = 2 AND
        NOT EXISTS (
            SELECT *
            FROM Messages M2
            WHERE
                M2.message_from_user_id = 2 AND
                M2.message_project_id = M.message_project_id AND
                M2.message_to_user_id = M.message_from_user_id AND
                M2.message_time >= M.message_time
        )
    

    This will get you all of the messages for all projects for the user where he hasn’t sent a message back to the sender for that project. You can add an ORDER BY of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This works: CREATE TABLE shoutbox_shout ( shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
create table snippet( id int not null auto_increment, primary key(id), idlanguage int not null,
Create Table: CREATE TABLE `category` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255)
create table ImagenesUsuario { idImagen int primary key not null IDENTITY } This doesn't
CREATE TABLE Posts { id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(200), url VARCHAR(200) }
CREATE TABLE Customer ( customerID int identity (500,20) CONSTRAINT . . dateCreated datetime DEFAULT
I have a SQL table: CREATE TABLE [UserTable] ( [ID] [int] NULL, [Name] [nvarchar](50)
create table person ( name varchar(15), attr1 varchar(15), attr2 varchar(1), attr3 char(1), attr4 int
CREATE TABLE #Report( Cell int, CellValue double) Error here DECLARE @Report TABLE ( Cell
CREATE TABLE SupplierQuote ( supplierQuoteID int identity (3504,2) CONSTRAINT supquoteid_pk PRIMARY KEY, PONumber int

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.