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.
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:
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 BYof course.