我有三张表,其中包含不同的用户记录,
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
我想知道所有名册jid用户名&#39; jwyvm_rdyt&#39;如果该消息包含任何名册jid,则它具有其真实姓名和消息计数。喜欢&#39; evn-az-3ju&#39;在消息54和78中,所以为该名册jid计算2。
非常感谢任何帮助。
答案 0 :(得分:0)
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