代码之家  ›  专栏  ›  技术社区  ›  froadie

为什么我不能从查询中选择?

  •  1
  • froadie  · 技术社区  · 13 年前

    我有一个类似这样的查询(不,我的表和字段不是由这些名称调用的,但是结构是相同的)-

    SELECT table1.id, table2.id, table1.phone1
    FROM table1 LEFT OUTER JOIN
        table3 ON table3.id = table1.id LEFT OUTER JOIN
        table2 ON table3.id2 = table2.id    
        WHERE table1.phone1 IS NOT NULL AND LTRIM(RTRIM(table1.phone1)) <> ''
    
    UNION
    
    SELECT table1.id, table2.id, table1.phone2
    FROM table1  LEFT OUTER JOIN
        table3 ON table3.id = table1.id LEFT OUTER JOIN
        table2 ON table3.id2 = table2.id    
    WHERE table1.phone2 IS NOT NULL AND LTRIM(RTRIM(table1.phone2)) <> ''
    
    UNION
    
    SELECT table1.id, table2.id, table2.phone
    FROM table1 LEFT OUTER JOIN
        table3 ON table3.id = table1.id LEFT OUTER JOIN
        table2 ON table3.id2 = table2.id    
    WHERE table2.phone IS NOT NULL AND LTRIM(RTRIM(table2.phone)) <> ''
    

    运行良好。但是,当我试图从中进行选择时(我最终将尝试使用group bys等操作,但目前只尝试了一个简单的选择),如下所示:

    SELECT * FROM
        ( SELECT table1.id, table2.id, table1.phone1
        FROM table1 LEFT OUTER JOIN
            table3 ON table3.id = table1.id LEFT OUTER JOIN
            table2 ON table3.id2 = table2.id    
            WHERE table1.phone1 IS NOT NULL AND LTRIM(RTRIM(table1.phone1)) <> ''
    
        UNION
    
        SELECT table1.id, table2.id, table1.phone2
        FROM table1  LEFT OUTER JOIN
            table3 ON table3.id = table1.id LEFT OUTER JOIN
            table2 ON table3.id2 = table2.id    
        WHERE table1.phone2 IS NOT NULL AND LTRIM(RTRIM(table1.phone2)) <> ''
    
        UNION
    
        SELECT table1.id, table2.id, table2.phone
        FROM table1 LEFT OUTER JOIN
            table3 ON table3.id = table1.id LEFT OUTER JOIN
            table2 ON table3.id2 = table2.id    
        WHERE table2.phone IS NOT NULL AND LTRIM(RTRIM(table2.phone)) <> '' )
    

    我知道错误:

    Incorrect syntax near ')'.
    

    我做错什么了?

    1 回复  |  直到 13 年前
        1
  •  8
  •   Matt Gibson    13 年前

    你错过了命名子选择。以类似的方式结束 ...) sub_query_name .