简体   繁体   中英

MySQL: how to delegate alias into subquery of subquery?

How can I delegate my DV_ID or DV.ID into the second (deeper) subquery? Is it possible? (If I'd have just one level of nesting it would work.)

SELECT DV.ID DV_ID, (
SELECT COUNT(*) FROM (SELECT * MYTABLE VSV
WHERE VSV.ID = DV_ID //DV_ID is not seen...
GROUP BY VSV.ID) A ) B
FROM MYTABLE2 DV

When you use an alias in SELECT field list is not seen in the subquery. You can use DV.ID (DV is alias of your table).

In your example:

SELECT DV.ID DV_ID

you can replace your output alias with a string, like this:

SELECT DV.ID "Hi my dear"

but is not correct read that information inside other part of query

You can rewrite your particular query as:

SELECT DV.ID as DV_ID,
       (SELECT COUNT(DISTINCT VSV.ID)
        FROM MYTABLE VSV
        WHERE VSV.ID = DV.ID //DV_ID is not seen...
       ) as B
FROM MYTABLE2 DV;

(NOTE: If VSV.ID can take on NULL values, the expression would be SELECT COUNT(DISTINCT vsv.ID) + MAX(vsv.id is null) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM