带派生的MySQL派生表

时间:2015-06-24 08:02:49

标签: mysql join derived-table

在以下查询中,我在返回count2的正确值时遇到了问题。

我需要的是从table2获取行数,这可以通过使用派生表t轻松完成:

SELECT name, 
(SELECT COUNT(*) FROM `table1`) AS count1,
(SELECT COUNT(*) FROM (
    SELECT COUNT(*) FROM `table2` t2) WHERE t2.user = prf.user)
) t AS count2,
(SELECT SUM(a) FROM `table3`) AS count3
FROM `profiles` prf
WHERE 1=1
AND prf.user = 1

问题是WHERE t2.user = prf.user语句失败,因为prf表在子查询的范围之外。

我如何实现上述目标?

编辑:我正在添加实际查询,以防有助于更好地掌握:

SELECT PRF.BranchID, PRF.user_id, CONCAT_WS(" ",PRF.lastname,PRF.firstname) Synergatis, 
( SELECT COUNT(*) FROM Actions A JOIN Requests R ON R.RequestID=A.RequestID WHERE A.ActionStatus = 302 AND A.UserOwner = PRF.user_id AND A.ActionDate BETWEEN '2015-06-01' AND '2015-06-10' ) AS energeies, 
( SELECT COUNT(DISTINCT RPP.RequestID) FROM VW_Xartofylakio_Synergati VV JOIN Requests_Prop RPP ON RPP.PropertyID = VV.PropertyID JOIN Requests R ON R.RequestID = RPP.RequestID WHERE VV.CurrUsr = PRF.user_id AND R.ModifyTime BETWEEN '2015-06-01' AND '2015-06-10' ) AS zitiseis_eidikes, 
( SELECT COUNT(DISTINCT(CustomerID)) FROM Demo_Orders_M WHERE DemoOrderStatus=253 AND USER=PRF.user_id AND DemoOrderDate BETWEEN '2015-06-01' AND '2015-06-10' ) AS endiaferomenoi, 
( SELECT COUNT(*) AS cnt FROM Demo_Orders_M DOM JOIN Actions A ON DOM.DemoOrderID = A.DemoOrderID WHERE DOM.User = PRF.user_id AND DOM.DemoOrderStatus = 253 AND A.ActionDate BETWEEN '2015-06-01 14:56:19' AND '2015-06-30 14:56:19' GROUP BY DOM.CustomerID, DOM.User HAVING COUNT(*) > 1 ) AS anakykl_endiaf, 
( SELECT COUNT(*) FROM Demo_Orders_M DOM WHERE DOM.`User`=PRF.user_id AND DemoOrderStatus = 253 AND DOM.DemoOrderDate BETWEEN '2015-06-01' AND '2015-06-10' ) AS epideixeis, 
( SELECT COUNT(DISTINCT(DOD.PropertyID)) AS PropertyID FROM Demo_Orders_M DOM JOIN Demo_Orders_D DOD ON DOM.DemoOrderID = DOD.DemoOrderID JOIN Actions A ON DOD.DemoOrderID = A.DemoOrderID WHERE DOM.DemoOrderStatus = 253 AND DOM.User = PRF.user_id AND A.ActionDate BETWEEN '2015-06-01' AND '2015-06-10' ) AS monadika_akinita
FROM tbl_profiles PRF 
WHERE 1=1 
AND PRF.user_id IN (
    SELECT a.user_id FROM tbl_profiles a WHERE a.user_id IN ('248','1159','486','183') 
    OR a.GroupID IN (SELECT b.GroupID FROM L_Groups b WHERE b.ManagerID IN ('248','1159','486','183')) 
    ) 
ORDER BY PRF.user_id

我所指的子查询是将结果作为anakykl_endiaf返回的子查询。

2 个答案:

答案 0 :(得分:1)

我怀疑它不是因为prf表,而是因为t2表...在内部子查询中使用外部别名没有限制,因为存在像相关子查询这样的东西。你的问题是你遇到了相反的情况:你在外部查询中引用内部别名。

(SELECT COUNT(*) 
 FROM (SELECT COUNT(*) FROM `table2` t2) WHERE t2.user = prf.user)

为什么你在这里选择计数两次?你可以改为:

(SELECT COUNT(*) 
 FROM (SELECT COUNT(*) FROM `table2` t2 WHERE t2.user = prf.user))

或者这个:

(SELECT COUNT(*) 
 FROM `table2` t2 WHERE t2.user = prf.user)

答案 1 :(得分:0)

建议尝试。

您在SELECT中有子查询,在这种情况下,它们每个都必须只返回一行。由于某种原因(我们无法在没有测试数据的情况下真正告知)其中一个返回超过1行,因此失败。

作为临时步骤,将查询更改为针对子查询进行连接,这应该会在存在重复时更加明显(并且根据数据可能也会更高效)。

像这样的东西(未经过测试,可能是一些错别字): -

SELECT PRF.BranchID, 
    PRF.user_id, 
    CONCAT_WS(" ",PRF.lastname,PRF.firstname) Synergatis, 
    ar.energeies,
    vrr.zitiseis_eidikes,
    m.endiaferomenoi, 
    ae.anakykl_endiaf,
    d.epideixeis,
    ddd.monadika_akinita
FROM tbl_profiles PRF 
LEFT OUTER JOIN
( 
    SELECT A.UserOwner AS DomUser, COUNT(*) AS energeies
    FROM Actions A 
    JOIN Requests R ON R.RequestID=A.RequestID 
    WHERE A.ActionStatus = 302 
    AND A.ActionDate BETWEEN '2015-06-01' AND '2015-06-10' 
    GROUP BY A.UserOwner
) ar 
ON ar.DomUser = PRF.user_id 
LEFT OUTER JOIN
( 
    SELECT VV.CurrUsr AS DomUser, COUNT(DISTINCT RPP.RequestID) AS zitiseis_eidikes
    FROM VW_Xartofylakio_Synergati VV 
    JOIN Requests_Prop RPP ON RPP.PropertyID = VV.PropertyID 
    JOIN Requests R ON R.RequestID = RPP.RequestID 
    WHERE R.ModifyTime BETWEEN '2015-06-01' AND '2015-06-10' 
    GROUP BY VV.DomUser
) vrr 
ON vrr.DomUser = PRF.user_id 
LEFT OUTER JOIN
( 
    SELECT `USER` AS DomUser, COUNT(DISTINCT(CustomerID)) AS endiaferomenoi
    FROM Demo_Orders_M 
    WHERE DemoOrderStatus=253 
    AND DemoOrderDate BETWEEN '2015-06-01' AND '2015-06-10' 
    GROUP BY DomUser
) m
ON PRF.user_id  = m.DomUser
LEFT OUTER JOIN
( 
    SELECT DOM.CustomerID, DOM.`User` AS DomUser, COUNT(*) AS anakykl_endiaf 
    FROM Demo_Orders_M DOM 
    JOIN Actions A ON DOM.DemoOrderID = A.DemoOrderID 
    WHERE DOM.DemoOrderStatus = 253 
    AND A.ActionDate BETWEEN '2015-06-01 14:56:19' AND '2015-06-30 14:56:19' 
    GROUP BY DOM.CustomerID, DOM.DomUser 
    HAVING COUNT(*) > 1 
) ae
ON PRF.user_id  = ae.DomUser
LEFT OUTER JOIN
( 
    SELECT DOM.`User` AS DomUser, COUNT(*) AS epideixeis
    FROM Demo_Orders_M DOM 
    WHERE DemoOrderStatus = 253 
    AND DOM.DemoOrderDate BETWEEN '2015-06-01' AND '2015-06-10' 
    GROUP BY DOM.DomUser 
) d 

修改

如果您只想在anakykl_endiaf字段中计算用户的customerID字段数,请将其更改为对不同的customerID进行计数。即,对于上述查询,我​​已将其更改为: -

LEFT OUTER JOIN
( 
    SELECT DOM.`User` AS DomUser, COUNT(DISTINCT DOM.CustomerID) AS anakykl_endiaf 
    FROM Demo_Orders_M DOM 
    JOIN Actions A ON DOM.DemoOrderID = A.DemoOrderID 
    WHERE DOM.DemoOrderStatus = 253 
    AND A.ActionDate BETWEEN '2015-06-01 14:56:19' AND '2015-06-30 14:56:19' 
    GROUP BY DOM.DomUser 
    HAVING COUNT(*) > 1 
) ae