//This is the conversation table where sender is the one who sends the message and the receiver is the one who receives the message and Context is the foreign key of another table
id Context Message Sender Receiver Date
1 3 Hello 1 3 2019-01-25
2 4 How Are you 3 2 2019-01-26
3 1 Are you there 1 6 2019-01-27
4 1 sample messages 5 1 2019-01-28
5 1 Where are you from 4 2 2019-01-29
6 5 Where are you now 1 3 2019-01-30
7 4 There you are 4 2 2019-01-31
8 5 What languages 5 4 2019-01-25
9 2 What language code 2 1 2019-01-26
10 6 Do you code? 2 5 2019-01-27
11 5 Do you have a car 2 5 2019-01-28
12 5 color of your car. 1 2 2019-01-29
13 5 I have a blue car 3 1 2019-01-30
14 2 Yes I have a car 1 5 2019-01-31
15 1 car maker is honda 5 3 2019-01-25
16 1 I have red color hat. 1 3 2019-01-26
17 6 What colored hat ? 3 2 2019-01-27
18 2 Do you use my sql 2 1 2019-01-28
19 4 Do u code in php 5 1 2019-01-29
20 1 Do you know java 1 2 2019-01-30
21 1 favourite key ? 2 1 2019-01-31
22 5 Mine is spacebar 4 1 2019-02-01
//This is the context table where the title is the title and posted by is the user who has posted this topic. We can connect id to context in the conversation table.
id title posted by
1 Lets talk about javascript 2
2 I have a blue car 1
3 I do not have a hat 5
4 How do you do 2
5 Where do you stay 4
6 Fault in our stars 1
//This is the user table from where we can retrieve information about the user for Sender, Receiver in the conversation table and posted by in the context table.
User Name Last Name Age Email
1 John Doe 24 john@gmail.com
2 Jane Dow 23 jane@gmail.com
3 Jack Daniels 41 jack@gmail.com
4 Peter Parker 29 spidy22@gmail.com
5 Tony Stark 39 ifly@gmail.com
6 Terri Benedict 49 terribenedict@gmail.com
现在,我想显示一个列表,以登录与他/她聊天的用户,其中包括主题名称,与他/她聊天的人的姓名,日期和最后一条消息。
我尝试过 ***假设已登录的用户为2。
在会话中选择DISTINCT(上下文)(发件人=“ 2”或接收者=“ 2”);
这可以使我进行所有对话,并且正确的右联接也可以为我提供名称和所有信息,无论我想如何获得他们对话中的最后一条消息 我在标记中添加了“ php”,以防万一在检索数据后需要进一步处理。
答案 0 :(得分:1)
您可以使用CTE建立给定用户在每种情况下的最大日期列表,然后JOIN
到conversation
,context
和user
表以获取每个上下文中最后一次对话的详细信息。例如,对于用户2
:
WITH usernum AS (
SELECT '2' AS user
),
max_user_date AS (
SELECT DISTINCT c.context, u.user, MAX(c.`Date`) OVER (PARTITION BY context, user) AS max_date
FROM conversation c
JOIN usernum u ON c.Sender = u.user OR c.Receiver = u.user
)
SELECT m.max_date as `Date`,
cx.title AS title,
cv.Message AS message,
CONCAT(u1.Name, ' ', u1.`Last Name`) AS Sender,
CONCAT(u2.Name, ' ', u2.`Last Name`) AS Receiver
FROM max_user_date m
JOIN conversation cv ON (cv.Sender = m.user OR cv.Receiver = m.user)
AND cv.context = m.context
AND cv.Date = m.max_date
JOIN context cx ON cx.id = cv.Context
JOIN user u1 ON u1.User = cv.Sender
JOIN user u2 ON u2.User = cv.Receiver
ORDER BY `Date`, cx.id
输出:
Date title message Sender Receiver
2019-01-27 Fault in our stars What colored hat ? Jack Daniels Jane Dow
2019-01-27 Fault in our stars Do you code? Jane Dow Tony Stark
2019-01-28 I have a blue car Do you use my sql Jane Dow John Doe
2019-01-29 Where do you stay color of your car. John Doe Jane Dow
2019-01-31 Lets talk about javascript favourite key ? Jane Dow John Doe
2019-01-31 How do you do There you are Peter Parker Jane Dow
如果您的数据库版本不支持CTE和窗口功能,则可以将CTE编写为子查询:
SELECT m.max_date as `Date`,
cx.title AS title,
cv.Message AS message,
CONCAT(u1.Name, ' ', u1.`Last Name`) AS Sender,
CONCAT(u2.Name, ' ', u2.`Last Name`) AS Receiver
FROM (
SELECT c.context, '2' AS user, MAX(c.`Date`) AS max_date
FROM conversation c
WHERE c.Sender = '2' OR c.Receiver = '2'
GROUP BY c.context
) m
JOIN conversation cv ON (cv.Sender = m.user OR cv.Receiver = m.user)
AND cv.context = m.context
AND cv.Date = m.max_date
JOIN context cx ON cx.id = cv.Context
JOIN user u1 ON u1.User = cv.Sender
JOIN user u2 ON u2.User = cv.Receiver
ORDER BY `Date`, cx.id
输出与第一个查询相同。 Demo on dbfiddle