2014-09-09 3 views
0

두 가지 쿼리가 있습니다.이 두 쿼리는 매우 잘 맞았습니다. 따라서이 두 메서드를 결합하는 방법을 찾지 못했습니다.이 두 가지 복잡한 쿼리를 어떻게 참여시킬 수 있습니까?

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) 

나는 것 그들을 결합하려고 항상 엉망하는

나는 두 쿼리의 Q1.notAnyFewID = Q2.FBID

가 이

Q1 때 결과 세트를 찾으려면 가입 또는 하위 선택 또는 "사용"또는 어디서 어떻게 새 정보를 얻을 수 있는지 알려줍니다.

쿼리 중 하나를 다른 쿼리의 결과와 비교하는 가장 좋은 방법은 무엇입니까? 다른 프로그래밍 언어에서 그렇게 할 이유

+0

에서 하위 쿼리로 두 개의 쿼리를 넣어? –

+0

Becuase 나는 데이터베이스에 대한 호출을 여러 개 만들고 싶지 않습니다. (테이블 중 일부는 꽤 커질 것이고 다양한 개별 쿼리는 많은 결과를 반환 할 수 있으므로 DB 호출과 함께 루프를 갖고 싶지는 않습니다) – Per

+0

이들은 복잡합니다. 둘의 결과에 합류하려고하십니까? 어떤 관계입니까, IE의 어느 열에 가입 하시겠습니까? 신경 쓰지 마, 방금 너의 편집을 봤어. 코드 블록을 정리해 주셔서 감사합니다. – BClaydon

답변

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 
+0

예, 이것이 제가 지금 시도하고있는 것입니다. 나는 지금 일하기 직전에 있다고 생각합니다. (나는 똑같은 코드를 가지고 있지만, 결과와 함께 뭔가 비린내가있는 것 같지만 현재 가지고있는 데이터를 확인해야한다) – Per