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`)
)
我希望retreive是每个项目的用户#2的最新消息。
用户#2是项目的所有者,因此可以接收来自许多相关方的消息。
实际页面将显示用户#2的“待办事项”列表。我想找到当前打开的项目的任何消息,其中用户#2已收到消息,但尚未发送一个
因此,如果用户#1向用户#2发送了一条消息,则消息表中会有一行
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
到目前为止我尝试了但是没有完成工作: //这将为我提供每个项目的最新消息,但不会被用户分开
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
//这将按用户分隔,但不会返回最新的消息文本
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
一旦数据返回Php i,检查最近的消息是否是TO用户#2,如果是,则将“你需要回复”消息发布到他的屏幕。
答案 0 :(得分:1)
如果您提供了一些涵盖大部分案例的示例输出,那可能会有所帮助。从您的评论来看,经过多次重读后,我认为这就是您所寻找的:
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
)
这将为用户提供所有项目的所有消息,他们尚未将消息发送回该项目的发件人。您当然可以添加ORDER BY
。
答案 1 :(得分:0)
这是否有效:
SELECT * FROM messages m LEFT JOIN project p ON (p.project_id = m.message_project_id) WHERE (m.message_from_user_id = 211 OR m.message_to_user_id = 211 ) AND p.project_status IN('open','started') AND p.project_user_id = 2 GROUP BY p.project_id ORDER BY m.message_time DESC
希望有所帮助