我有两个不同的查询单独运作。第一个给了我有用的结果列TOTALI
和第二个查询列RIMBORSATI
。所以我需要将第一个查询与第二个查询联合起来,并使第一个查询的HAVING子句像HAVING totali-rimborsati < professionisti.limite
这样的操作。
非常感谢你。
首先查询:
SELECT professionisti.*,COUNT(contatti_acquistati_addebito.email) AS totali
FROM professionisti
LEFT JOIN contatti_acquistati_addebito ON
professionisti.email = contatti_acquistati_addebito.email
AND contatti_acquistati_addebito.DATA
BETWEEN ('2014-05-01') AND ('2014-05-31')
WHERE professionisti.categoria LIKE '%0540%' AND
professionisti.province LIKE '%MI%'
AND professionisti.addebito='1'
GROUP BY professionisti.email
HAVING totali < professionisti.limite
ORDER BY totali ASC LIMIT 4
第二次查询:
SELECT professionisti.*,COUNT(contatti_rimborsi.email) AS rimborsati
FROM professionisti
LEFT JOIN contatti_rimborsi ON professionisti.email = contatti_rimborsi.email AND
contatti_rimborsi.DATA BETWEEN ('2014-05-01') AND ('2014-05-31')
WHERE professionisti.categoria LIKE '%0540%'
AND professionisti.province LIKE '%MI%'
AND professionisti.addebito='1'
GROUP BY professionisti.email
ORDER BY totali ASC LIMIT 4
答案 0 :(得分:0)
select sq1.*,sq2.rimborsati
from
(SELECT professionisti.email as email, professionisti.categoria as categoria,professionisti.province as province,professionisti.addebito as addebito, COUNT(contatti_acquistati_addebito.email) AS totali
FROM professionisti
LEFT JOIN contatti_acquistati_addebito ON
professionisti.email = contatti_acquistati_addebito.email
AND contatti_acquistati_addebito.DATA
BETWEEN ('2014-05-01') AND ('2014-05-31')
WHERE professionisti.categoria LIKE '%0540%' AND
professionisti.province LIKE '%MI%'
AND professionisti.addebito='1'
GROUP BY professionisti.email
HAVING totali < professionisti.limite
ORDER BY totali ASC LIMIT 4) sq1,
(SELECT professionisti.email as email, professionisti.categoria as categoria,professionisti.province as province,professionisti.addebito as addebito, COUNT(contatti_rimborsi.email) AS rimborsati
FROM professionisti
LEFT JOIN contatti_rimborsi ON professionisti.email = contatti_rimborsi.email AND
contatti_rimborsi.DATA BETWEEN ('2014-05-01') AND ('2014-05-31')
WHERE professionisti.categoria LIKE '%0540%'
AND professionisti.province LIKE '%MI%'
AND professionisti.addebito='1'
GROUP BY professionisti.email
ORDER BY totali ASC LIMIT 4) sq2
where sq1.email=sq2.email and totali <rimborsati
答案 1 :(得分:0)
select t1.email,t1.limite,t1.totali,t2.rimborsati
from (
SELECT professionisti.email,
max(professionisti.limite) as limite,
min(COUNT(contatti_acquistati_addebito.email) AS totali
FROM professionisti
LEFT JOIN contatti_acquistati_addebito ON
professionisti.email = contatti_acquistati_addebito.email
AND contatti_acquistati_addebito.DATA
BETWEEN ('2014-05-01') AND ('2014-05-31')
WHERE professionisti.categoria LIKE '%0540%' AND
professionisti.province LIKE '%MI%'
AND professionisti.addebito='1'
GROUP BY professionisti.email
-- Here that professionisti.limite does make sense to me it should be an aggregate function!?
-- (are you sure this query works?)
-- using max(professionisti.limite) and using the aggregate count for email
HAVING COUNT(contatti_acquistati_addebito.email) < max(professionisti.limite)
-- using aggregate more general sql (works better on other engines)
-- removed see why below.
-- ORDER BY COUNT(contatti_acquistati_addebito.email) ASC LIMIT 4
) t1
left join (
SELECT professionisti.email,COUNT(contatti_rimborsi.email) AS rimborsati
FROM professionisti
LEFT JOIN contatti_rimborsi ON professionisti.email = contatti_rimborsi.email AND
contatti_rimborsi.DATA BETWEEN ('2014-05-01') AND ('2014-05-31')
WHERE professionisti.categoria LIKE '%0540%'
AND professionisti.province LIKE '%MI%'
AND professionisti.addebito='1'
GROUP BY professionisti.email
-- Here you cannot order by totali you do not have it so I am removing both order by
-- alternativly put the same left join with contatti_acquistati_addebito as above!
-- ORDER BY totali ASC LIMIT 4
) t2 on t1.email=t2.email
where ,t1.totali-t2.rimborsati < t1.limite
答案 2 :(得分:0)
SELECT p.*,m1.*,m2.*,IFNULL(m2.rimborsi, 0) as rimborsiok
FROM professionisti p
LEFT JOIN
(
SELECT ca.email, COUNT(*) AS totali
FROM contatti_acquistati_addebito ca
WHERE ca.data between ('2014-06-01') AND ('2014-06-31')
GROUP BY ca.email
) AS m1 ON p.email = m1.email
LEFT JOIN
(
SELECT cr.email, COUNT(*) AS rimborsi
FROM contatti_rimborsi cr
WHERE cr.data between ('2014-06-01') AND ('2014-06-31')
GROUP BY cr.email
) AS m2 ON p.email = m2.email
WHERE p.categoria LIKE '%0540%' AND p.province LIKE '%MI%' AND p.standby='0' AND p.addebito='1'
HAVING m1.totali-rimborsiok<p.limite OR p.limite=0