我在查询时遇到问题,也许有人可以帮助我:)
我有3张桌子:
1)文件
DocRef CustRef
Doc1 Cust1
Doc2 Cust2
Doc3 Cust1
2)LinkInter
DocRef InterRef
Doc1 Inter1
Doc2 Inter2
Doc3 Inter3
3)Inter
id InterRef deliverychannel email mobilenb date status
1 Inter1 Email email1@email.be 2013-01-14T00:00:00Z ok
2 Inter1 SMS 0444111111 2013-01-12T00:00:00Z ko
3 Inter1 Other email5@email.be 2013-02-21T00:00:00Z ko
4 Inter2 Email email2@email.be 044456465465 2013-01-21T00:00:00Z ko
5 Inter3 Email 2013-01-21T00:00:00Z ko
6 Inter3 SMS 2013-01-22T00:00:00Z ko
7 Inter3 Other email3@mail.be 2013-01-22T00:00:00Z ko
要获得最佳视图,请点击此处:sqlfiddle
我想要的结果如下:
CustRef | number of InterRef SMS KO| Last email known| Last mobilenb known
Cust1 | 2 | email5@email.be | 0444111111
我希望所有的客户的交付渠道=“ SMS”,状态为“ ko”,状态为“ KO”的Inter(交互)数,最近的电子邮件已知,最后的移动电话号码。
我尝试使用2个函数getLastEmailKnown和getLastmobilenbKnown,但是我的表计数的记录超过10.000.000,这需要很多时间
我可以在php页面上获得此结果,但这不是解决方案,我只希望有一个查询
如果我分别执行3个查询,它可以工作,但我只想要一个
SELECT d.CustRef, count(DISTINCT i.InterRef) , i.email, i.mobilenb
FROM Documents d
INNER join LinkInter link on link.DocRef = d.DocRef
INNER join Inter i on i.InterRef = link.InterRef
and i.deliverychannel = 'SMS'
and i.status = 'ko'
group by d.CustRef
Order by d.CustRef DESC;
SELECT distinct i.email as lastEmailKnown, d.CustRef
FROM Inter i
INNER join LinkInter link on link.InterRef = i.InterRef
INNER join Documents d on d.DocRef = link.DocRef
where d.CustRef = 'Cust1' AND email is not NULL and email <> '' ORDER by date DESC LIMIT 1;
SELECT distinct i.mobilenb as lastmobileKnown, d.CustRef
FROM Inter i
INNER join LinkInter link on link.InterRef = i.InterRef
INNER join Documents d on d.DocRef = link.DocRef
where d.CustRef = 'Cust1' AND mobilenb is not NULL and mobilenb <> '' ORDER by date DESC LIMIT 1;