How to specify the parent query field from within a subquery in mySQL?

前端 未结 6 1018
心在旅途
心在旅途 2020-12-03 02:24

Is there a way to specify the parent query field from within a subquery in mySQL?

For Example:
I have written a basic Bulletin Board type progra

6条回答
  •  再見小時候
    2020-12-03 03:01

    Thanks Don. I had a nested query as shown below and a WHERE clause in it wasn't able to determine alias v1. Here is the code which isn't working:

    Select 
        teamid,
        teamname
    FROM
        team as t1
    INNER JOIN (
        SELECT 
            venue_id, 
            venue_scores, 
            venue_name 
        FROM venue 
        WHERE venue_scores = (
            SELECT 
                MAX(venue_scores) 
            FROM venue as v2 
            WHERE v2.venue_id = v1.venue_id      /* this where clause wasn't working */
        ) as v1    /* v1 alias already present here */
    );
    

    So, I just added the alias v1 again inside the JOIN. Which made it work.

    Select 
        teamid,
        teamname
    FROM
        team as t1
    INNER JOIN (
        SELECT 
            venue_id, 
            venue_scores, 
            venue_name 
        FROM venue as v1              /* added alias v1 here again */
        WHERE venue_scores = (
            SELECT 
                MAX(venue_scores) 
            FROM venue as v2 
            WHERE v2.venue_id = v1.venue_id   /* Now this works!! */
        ) as v1     /* v1 alias already present here */
    );
    

    Hope this will be helpful for someone.

提交回复
热议问题