0

我有两个查询,它们非常复杂,所以我似乎无法弄清楚如何加入它们。

我想Q1.notAnyFewID = Q2.FBID从两个查询中找到结果集

Q1:

SELECT DISTINCT notifications.`receiver` AS notAnyFewID
FROM notifications
JOIN 
(SELECT notifications.`ref` AS notRef, notifications.`receiver` AS recI
    FROM notifications
    WHERE notifications.`ref`='tooFewLandings') AS c
ON notifications.`receiver`=c.recI
WHERE notifications.`receiver`!=c.recI

Q2:

SELECT DISTINCT R2PProfiles.id AS r2pID, R2PProfiles.`facebookID` AS FBID
FROM R2PProfiles
LEFT JOIN
      (SELECT COUNT(*) AS Landings, R2PProfiles.facebookID, R2PProfiles.id
            FROM pageTrack         
            JOIN (R2PProfiles)
        ON (pageTrack.inputRefNum = R2PProfiles.id)
WHERE pageTrack.ref='getProfile-Land' AND R2PProfiles.published=2 AND R2PProfiles.`createTime`< NOW()- INTERVAL 24 HOUR GROUP BY R2PProfiles.id) AS h
USING (id) WHERE (Landings < 20)

当试图将它们结合起来时,我似乎总是在加入或子选择或“使用”或在哪里以及如何在正确的地方获得新的东西时搞砸了。

将其中一个查询与另一个查询的结果进行比较的最佳方法是什么?

4

1 回答 1

3

只需将两个查询作为子查询放在JOIN

SELECT notAnyFewID, r2pID
FROM (SELECT DISTINCT notifications.`receiver` AS notAnyFewID
        FROM notifications
        JOIN 
        (SELECT notifications.`ref` AS notRef, notifications.`receiver` AS recI
            FROM notifications
            WHERE notifications.`ref`='tooFewLandings') AS c
        ON notifications.`receiver`=c.recI
        WHERE notifications.`receiver`!=c.recI) AS q1
JOIN (SELECT DISTINCT R2PProfiles.id AS r2pID, R2PProfiles.`facebookID` AS FBID
        FROM R2PProfiles
        LEFT JOIN
              (SELECT COUNT(*) AS Landings, R2PProfiles.facebookID, R2PProfiles.id
                    FROM pageTrack         
                    JOIN (R2PProfiles)
                ON (pageTrack.inputRefNum = R2PProfiles.id)
        WHERE pageTrack.ref='getProfile-Land' AND R2PProfiles.published=2 AND R2PProfiles.`createTime`< NOW()- INTERVAL 24 HOUR GROUP BY R2PProfiles.id) AS h
        USING (id) WHERE (Landings < 20)) AS q2 
ON q1.notAnyFewID = q2.FBID
于 2014-09-09T18:37:41.717 回答