1
mysql> Select Emp_B AS Total
    -> From (Select Sum(mines.NoOfWorkers) AS Emp_B from mines);
ERROR 1248 (42000): Every derived table must have its own alias

mysql> Select Emp_B AS Total
    -> From (Select Sum(mines.NoOfWorkers) from mines) AS Emp_B;
ERROR 1054 (42S22): Unknown column 'Emp_B' in 'field list'

我对这个 SQL 语句有一些问题。任何帮助将不胜感激

4

3 回答 3

1
Select Emp_B AS Total
From (Select Sum(mines.NoOfWorkers) AS Emp_B from mines) x;

由于错误状态Every derived table must have its own alias 只是给它一个别名,就像x上面一样。OR AS x,但AS该词是可选的。

或者为什么别名它两次...

Select Total
From (Select Sum(mines.NoOfWorkers) AS Total from mines) x;

但是由于 SUM 只给你一个值,除非你从某个更大的值简化了查询,这会给出完全相同的结果??

Select Sum(mines.NoOfWorkers) AS Total from mines;
于 2011-03-27T22:58:23.643 回答
1

这应该适合你;但是,如果您只是将这个子选择放入临时表中,那么将其包装到另一个选择中是一种浪费,但这只是恕我直言。

Select Emp_B.sum From (Select Sum(mines.NoOfWorkers) as sum from mines) AS Emp_B;
于 2011-03-27T22:58:38.303 回答
0
Select temp.total  
From 
  ( Select Sum(mines.NoOfWorkers) AS total 
    from mines
  ) AS temp
;
于 2011-03-27T23:01:39.727 回答