2016-05-30 4 views
0

我幾個小時試圖進行查詢,但我沒有成功! :(MySQL - 選擇查詢

與此查詢我選擇users_id併購買了由關鍵用戶的purchase_date

SELECT user_id, purchase_date FROM purchases AS p 
INNER JOIN keys AS pc 
ON p.transaction_id = pc.unique_id 
WHERE pc.generator_id = 96768 
GROUP BY user_id 

現在我想從這些用戶後,該誰買選擇 例如:SELECT * FROM purchases WHERE user_id = {users of query above} AND purchase_date > {purchase_date of query above}

回答

0

你可以加入子查詢表:

SELECT pr.* 
FROM purchases pr 
INNER JOIN ( 
    SELECT p.user_id, p.purchase_date 
    FROM purchases p 
    INNER JOIN keys pc ON p.transaction_id = pc.unique_id 
    WHERE pc.generator_id = 96768 
    GROUP BY p.user_id 
) p0 ON pr.user_id = p0.user_id AND pr.purchase_date > p0.purchase_date 

信息:MySQL JOIN Syntax