2

我的存储过程需要这个条件,我可以从上下文之外以任何方式访问在内部级别声明的别名吗

这有效: -

SELECT Count(*)
FROM   (SELECT id
        FROM   postdata
        WHERE  id IN ( 4, 5, 6 )
        UNION
        SELECT id
        FROM   (SELECT id,
                       date_completed
                FROM   postdata
                WHERE  id IN ( 1, 2, 3 ))a
        WHERE  a.date_completed in  ( "2012-1-10", "2012-10-02" ) )b  

有没有可能使这项工作

SELECT Count(*)
FROM   (SELECT id
        FROM   postdata
        WHERE  id IN ( 4, 5, 6 )
        UNION
        SELECT id
        FROM   (SELECT id,
                       date_completed
                FROM   postdata
                WHERE  id IN ( 1, 2, 3 ))a)b
WHERE  a.date_completed IN ( "2012-1-10", "2012-10-02" )  
4

1 回答 1

1

AFAIK,没有办法访问内部别名a

您必须在内部查询中添加判别式:

SELECT Count(*)
FROM   (SELECT 'a' AS discriminant,
               id
        FROM   postdata
        WHERE  id IN ( 4, 5, 6 )
        UNION
        SELECT 'b' AS discriminant,
               id
        FROM   (SELECT id,
                       date_completed
                FROM   postdata
                WHERE  id IN ( 1, 2, 3 ))a)b
WHERE  b.discriminant= 'a'
        OR ( b.discriminant= 'b'
             AND b.id IN ( 1, 2 ) )  

看到这个小提琴

于 2013-06-14T07:46:45.003 回答