Referencing outer query's tables in a subquery

前端 未结 5 1107
太阳男子
太阳男子 2020-12-14 05:20

Is it possible to reference an outer query in a subquery with MySQL? I know there are some cases where this is possible:

SELECT *
FROM table t1
WHER         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 06:09

    Isn't this what you're after?

    SELECT u.username, c._postCount
    FROM User u
    INNER JOIN (
        SELECT p.user, COUNT(*) AS _postCount
        FROM Posting p
        GROUP BY p.user    
    ) c ON c.user = u.id
    WHERE u.joinDate < '2009-10-10';
    

    The reason this will work is that the nature of the join itself will filter on user. You don't need to have a WHERE clause explictly filtering on user.

提交回复
热议问题