简体   繁体   English

MySQL全外部联接3个表,按组和计数

[英]Mysql full outer join 3 tables with group by and count

My query is only showing a subset of records of people who have messages for me whereas I want to return a list of all users wether they have messages for me or not along with the count (or true/false, 0,1) 我的查询仅显示了对我有消息的人的记录的子集,而我想返回所有用户的列表,无论他们是否对我有消息以及计数(或true / false,0,1)

SELECT attendee.id, attendee.firstName, attendee.lastName, count(attendee_chat.to) 
from attendee_chat
INNER JOIN attendee ON attendee.id = attendee_chat.to
INNER JOIN chat ON attendee_chat.id = chat.attendee_chat_id
WHERE attendee.id <> 1
GROUP BY attendee_chat.to;

picture is worth a thousand words. 图片值一千个字。

在此处输入图片说明

If you want to do a join on records even if they have no matching element(s) in the other relation, you probably want an OUTER JOIN instead of an INNER JOIN. 如果您想要对记录进行联接,即使它们在另一个关系中没有匹配的元素,您也可能希望使用OUTER JOIN而不是INNER JOIN。

MySQL: Quick breakdown of the types of joins MySQL:联接类型的快速细分

Put the attendee table first in the FROM, and then left join to the other tables that may not have records. 首先将与会者表放在FROM中,然后将其联接到可能没有记录的其他表。

SELECT attendee.id, attendee.firstName, attendee.lastName, count(attendee_chat.to) 
from attendee 
LEFT JOIN attendee_chat ON attendee_chat.to = attendee.id
LEFT JOIN chat ON attendee_chat.id = chat.attendee_chat_id
WHERE attendee.id <> 1
GROUP BY attendee_chat.to;

I would avoid doing a FULL OUTER JOIN as it is not necessary and may lead to some unexpected results. 我将避免进行FULL OUTER JOIN,因为这是不必要的,并且可能导致某些意外结果。

EDIT: Try this. 编辑:试试这个。 Remove the chat table as you aren't selecting from it anyway, and group on the fields from the primary table attendee , rather than the on tables that may not contain any data. 删除chat表,因为您始终没有从中进行选择,而是对主要表attendee的字段进行分组,而不是对可能不包含任何数据的表进行分组。

SELECT attendee.id, attendee.firstName, attendee.lastName, count(attendee_chat.to) 
from attendee 
LEFT JOIN attendee_chat ON attendee_chat.to = attendee.id
WHERE attendee.id <> 1
GROUP BY attendee.id, attendee.firstName, attendee.lastName;

EDIT2: After discussion of requirements that he wants a count of chats that were to him (#1) EDIT2:在讨论了他想要对他进行聊天的数量的要求后(第1个)

SELECT attendee.id, attendee.firstName, attendee.lastName, 
(SELECT count(1) FROM attendee_chat 
   WHERE attendee_chat.from = attendee.id and  attendee_chat.to = 1) as chatcount
 from attendee ;

EDIT3: After comment about external ID EDIT3:关于外部ID的评论后

SELECT attendee.id, attendee.firstName, attendee.lastName, attendee.attendee_id,
(SELECT count(1) FROM attendee_chat 
    INNER JOIN attendee a2 ON a2.id = attendee_chat.toid
    WHERE attendee_chat.fromid = attendee.id and a2.attendee_id = 123
) as chatcount
FROM attendee ;

PS I've changed from to fromid and to -> toid as it is dangerous using reserved words as object names ;-) SQL Fiddle here PS我已经从toid更改为-> toid,因为使用保留字作为对象名称很危险;-) SQL Fiddle这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM