我为聊天系统准备了3张桌子:
这个设置对我来说非常有用,但现在我想做一个反向的'抬头。我知道哪些用户将开始新的聊天,我想知道这个特定的群组是否已经在数据库中进行了对话。有没有办法根据动态的外部行集找到一行?
(最好没有像技巧那样的支点)
或者我的数据库设计存在缺陷,我应该改变它吗?
CONVERSATION
id int auto_increment
start timestamp
CONVERSATION_PARTICIPANT
conversation_id int (foreign key to conversation)
participant_id int (foreign key to users table)
CONVERSATION_MESSAGE
id int auto_increment
conversation_id int (foreign key to conversation)
author_id int
time timestamp
message text
答案 0 :(得分:1)
这假设你:
用实际值替换那些伪变量
您可以在此处查看代码:http://sqlfiddle.com/#!2/e90f2/11
代码:
SELECT conversation_participant.conversation_id AS conversation_id
, SUM(IF(members.participant_id IN ($list),1,0)) AS member_count
, COUNT(*) AS total
FROM conversation_participant
JOIN conversation_participant AS members
ON members.conversation_id = conversation_participant.conversation_id
WHERE conversation_participant.participant_id = $participant
GROUP BY conversation_id
HAVING member_count = total
AND member_count = $qty;
仅供参考:WHERE子句的目的是限制潜在对话的数量。