简体   繁体   English

MySQL Select-将COUNT与GROUP_CONCAT结合

[英]MySQL Select - Combine COUNT with GROUP_CONCAT

I currently have a MySQL SELECT statement that pulls information from two tables to display a "Sent Messages" field for private messaging. 我目前有一个MySQL SELECT语句,该语句从两个表中提取信息以显示用于私人消息传递的“已发送消息”字段。

I'd like to know how I can do add a COUNT to my query to count the number of "receivers" in one thread. 我想知道如何在我的查询中添加一个COUNT来计算一个线程中“接收者”的数量。

Here's the basic gist of my table structure, (NOTE: the relational tie between the two tables is the 'message-id' in pm-info and the "id" in pm_data): 这是我的表结构的基本要点(注意:两个表之间的关系是pm-info中的“ message-id”和pm_data中的“ id”):

pm_info:
   id     message_id    receiver_id    is_read     read_date

pm_data:
   id    date_sent    title    sender_id    thread_id    content

Here's my SELECT Statement: 这是我的SELECT语句:

SELECT pm_info.is_read, group_concat(DISTINCT receiver.usrFirst) as receiver_name,
pm_data.date_sent, pm_data.title, pm_data.thread_id, pm_data.id as data_id,
MAX(date_sent) AS thread_max_date_sent
FROM pm_info
INNER JOIN pm_data ON pm_info.message_id = pm_data.id
INNER JOIN tblUsers AS receiver ON pm_info.receiver_id = receiver.usrID
WHERE pm_data.sender_id = '$usrID'
GROUP BY pm_data.thread_id
ORDER BY thread_max_date_sent DESC

And it ouputs the recipients like this: 它这样输出接收者:

Message 1 - Recipients: John, David, Steve - thread_id = 1234
Message 2 - Recipients: Bill, David, John, Ed, Steve - thread_id = 1345

Basically, what I'd like to do is have the option to COUNT the recipients, so in the example above, "Message 1" would display three (3) recipients, and "Message 2" would display five (5) recipients. 基本上,我想做的是可以选择对COUNT个收件人进行计数,因此在上面的示例中,“消息1”将显示三(3)个收件人,而“消息2”将显示五(5)个收件人。

This way, if I have a thread/message that was sent to 30 users, not all thirty names will be printed out. 这样,如果我有一个发送给30个用户的线程/消息,则不会打印出全部三十个名称。

Thanks! 谢谢!

Have you tried just replacing GROUP_CONCAT with COUNT? 您是否尝试过仅用COUNT替换GROUP_CONCAT? Like this: 像这样:

COUNT(DISTINCT receiver.usrFirst) as receiver_count

Or better, use pm_info.receiver_id as in (which could potentially let you eliminate one of your joins): 或更好的方法是,使用pm_info.receiver_id(可能会使您消除其中一个联接):

COUNT(DISTINCT pm_info.receiver_id) as receiver_count

You could use a subquery to select up to 3 recipients per message, and then another subquery to concatenate their names: 您可以使用一个子查询来选择每封邮件最多3个收件人,然后使用另一个子查询来连接其名称:

select 
    (   select group_concat(usrFirst separator ', ') 
        from tblUsers
        where usrId in (
            select usrId
            from tblUsers
            where usrId = pm_info.receiver_id
            limit 3
        )
    ) as receiver_name
 from pm_info
 ....

You could move the count and group_concat into a concat statement. 您可以将count和group_concat移到concat语句中。

... group_concat(DISTINCT receiver.usrFirst) as receiver_name ...

becomes

... concat('messages:', count(distinct receiver.usrFirst),'(',group_concat(DISTINCT receiver.usrFirst),')') as receiver_name ...

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

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