gpt4 book ai didi

Mysql查询获取计数结果并分组依据

转载 作者:行者123 更新时间:2023-11-29 20:17:54 25 4
gpt4 key购买 nike

我有三个表,其中包含不同的用户记录,

User

username | realname | date
evn-az-3ju john 11/2012 03:09:40 p.m.
jwyvm_rdyt steve 12/2012 03:09:40 p.m.
bsMIAtWkhi mahesh 01/2013 03:09:40 p.m.
zrObzh4um0 santa 01/2013 03:09:40 p.m.
WyVm_rDYt grolsch 11/2012 03:09:40 p.m.

offline

username | messageID | message
jwyvm_rdyt 54 <message to="jwyvm_rdyt" id="t4Wa4-291" type="chat" from="evn-az-3ju"><body>test1</body><thread>1a327531-5a1c-4d6b-8b66-1209cdabb77d</thread></message>
jwyvm_rdyt 78 <message to="jwyvm_rdyt" id="t4Wa4-290" type="chat" from="evn-az-3ju"><body>Happy birthday</body><thread>1a327531-5a1c-4d6b-8b66-1209cdabb77d</thread></message>
evn-az-3ju 89 <message to="evn-az-3ju" id="t4Wa4-290" type="chat" from="evn-az-3ju"><body>Happy birthday</body><thread>1a327531-5a1c-4d6b-8b66-1209cdabb77d</thread></message>
zrObzh4um0 98 <message to="zrObzh4um0" id="t4Wa4-290" type="chat" from="evn-az-3ju"><body>Happy birthday</body><thread>1a327531-5a1c-4d6b-8b66-1209cdabb77d</thread></message>
WyVm_rDYt 45 <message to="WyVm_rDYt" id="t4Wa4-290" type="chat" from="evn-az-3ju"><body>Happy birthday</body><thread>1a327531-5a1c-4d6b-8b66-1209cdabb77d</thread></message>

roster

username | jid | date
jwyvm_rdyt evn-az-3ju 11/2012 03:09:40 p.m.
jwyvm_rdyt zrObzh4um0 12/2012 03:09:40 p.m.
jwyvm_rdyt bsMIAtWkhi 01/2013 03:09:40 p.m.
zrObzh4um0 WyVm_rDYt 01/2013 03:09:40 p.m.
WyVm_rDYt zrObzh4um0 11/2012 03:09:40 p.m.

这些是我想要这样的结果集的表,

 username |             realname       |     count
evn-az-3ju john 2
zrObzh4um0 santa 0
bsMIAtWkhi mahesh 0

我想知道用户名“jwyvm_rdyt”拥有的所有名册 jid 及其真实姓名和消息数量(如果该消息包含任何名册 jid)。就像消息 54 和 78 中的“evn-az-3ju”一样,因此对该花名册 jid 计数 2。

任何帮助将不胜感激。

最佳答案

select c.jid as username,c.realname,COUNT(NULLIF(TRIM(o.message), ''))  as count from  (select r.`jid`,u.realname from roster r join User u on r.jid=u.username  where r.username='jwyvm_rdyt' ) c  left  join offline o on o.message  like CONCAT('%', c.jid, '%') and o.username='jwyvm_rdyt' group by jid

关于Mysql查询获取计数结果并分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39643230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com