为什么此查询不起作用并说无效列名称

时间:2017-04-17 09:45:16

标签: sql select sybase union-all

SELECT ROUND(K1,2)
FROM (SELECT '5.66666' UNION ALL
      SELECT '5.77777' UNION ALL
      SELECT '5.88888' UNION ALL
      SELECT '5.99999' UNION ALL
      SELECT '6.66666' UNION ALL
      SELECT '7.66666' UNION ALL
      SELECT '8.66666' UNION ALL
      SELECT '9.66666' UNION ALL
      SELECT '9.55555' UNION ALL 
      SELECT '6.88888' AS K1) K

错误说Invalid Column name 'K1'

3 个答案:

答案 0 :(得分:2)

union [all]结构中的列别名来自第一个查询,而不是最后一个。在那里移动你的别名,你应该没问题:

SELECT ROUND(K1,2)
FROM (SELECT 5.66666 AS K1 UNION ALL -- Here!
      SELECT 5.77777 UNION ALL
      SELECT 5.88888 UNION ALL
      SELECT 5.99999 UNION ALL
      SELECT 6.66666 UNION ALL
      SELECT 7.66666 UNION ALL
      SELECT 8.66666 UNION ALL
      SELECT 9.66666 UNION ALL
      SELECT 9.55555 UNION ALL 
      SELECT 6.88888) K

答案 1 :(得分:0)

为第一条记录提供列名,然后为记录使用相同的名称。

SELECT ROUND(K1,2)
FROM (SELECT '5.66666' AS K1 UNION ALL
      SELECT '5.77777' UNION ALL
      SELECT '5.88888' UNION ALL
      SELECT '5.99999' UNION ALL
      SELECT '6.66666' UNION ALL
      SELECT '7.66666' UNION ALL
      SELECT '8.66666' UNION ALL
      SELECT '9.66666' UNION ALL
      SELECT '9.55555' UNION ALL 
      SELECT '6.88888') K

答案 2 :(得分:0)

为第一列的第一行指定别名,或为所有行指定别名 第一栏:

   SELECT ROUND(K1,2)
   FROM 
   (
      SELECT '5.66666' AS K1 UNION ALL
      SELECT '5.77777' AS K1 UNION ALL
      SELECT '5.88888' AS K1 UNION ALL
      SELECT '5.99999' AS K1 UNION ALL
      SELECT '6.66666' AS K1 UNION ALL
      SELECT '7.66666' AS K1 UNION ALL
      SELECT '8.66666' AS K1 UNION ALL
      SELECT '9.66666' AS K1 UNION ALL
      SELECT '9.55555' AS K1 UNION ALL 
      SELECT '6.88888' AS K1
    ) K