我正在尝试执行嵌套选择查询,但我不断收到相同的错误,指出每个表必须有自己的别名。
SELECT
Sum(SumOfASSIGN_HOURS) AS SumOfSumOfASSIGN_HOURS,
Sum(SumOfASSIGN_CHARGE) AS SumOfSumOfASSIGN_CHARGE
FROM (
SELECT ASSIGNMENT.PROJ_NUM, SUM(ASSIGNMENT.ASSIGN_HOURS) AS SumOfASSIGN_HOURS,
SUM(ASSIGNMENT.ASSIGN_CHARGE) AS SumOfASSIGN_CHARGE
FROM (ASSIGNMENT)
GROUP BY ASSIGNMENT.PROJ_NUM);
我是 MYSQL 新手,我不知道我做错了什么。
最佳答案
这里的问题是您创建了一个派生表(子查询),并且必须为其指定别名 AS aliasName
或任何内容。只需在您的子查询中提供一个别名,如下所示,就可以了。
SELECT
Sum(aliasName.SumOfASSIGN_HOURS) AS SumOfSumOfASSIGN_HOURS,
Sum(aliasName.SumOfASSIGN_CHARGE) AS SumOfSumOfASSIGN_CHARGE
FROM(
SELECT ASSIGNMENT.PROJ_NUM, SUM(ASSIGNMENT.ASSIGN_HOURS) AS
SumOfASSIGN_HOURS,SUM(ASSIGNMENT.ASSIGN_CHARGE) AS SumOfASSIGN_CHARGE
FROM ASSIGNMENT
GROUP BY ASSIGNMENT.PROJ_NUM
) aliasName;
您可以在官方文档 here 上看到更多内容其中指出
Subqueries are legal in a SELECT statement's FROM clause. The actual syntax is:
SELECT ... FROM (subquery) [AS] name ...
The [AS] name clause is mandatory, because every table in a FROM clause must have a name. Any columns in the subquery select list must have unique names.
关于MYSQL每个表必须有自己的别名错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016852/