每个派生表都需要一个别名

时间:2012-10-27 05:00:13

标签: sql

这是我的代码,我需要每个部门薪水最低的教师的全名和薪水。我的子查询可以自己运行,但我不能让其余的一起工作。

SELECT CONCAT(FName,' ',LName) AS 'Faculty',DepartmentID,Salary
FROM Faculty,
(SELECT DISTINCT DepartmentID AS 'Department', MIN(Salary) AS 'MinSalary'
FROM Faculty GROUP BY DepartmentID)
WHERE Faculty.DepartmentID= 'Department' AND Salary= 'MinSalary'
ORDER BY DepartmentID

6 个答案:

答案 0 :(得分:3)

你需要在你的子查询上设置别名

SELECT CONCAT(FName,' ',LName) AS 'Faculty',DepartmentID,Salary
FROM   Faculty,
       (
        SELECT DISTINCT DepartmentID AS 'Department', 
               MIN(Salary) AS 'MinSalary'
        FROM Faculty GROUP BY DepartmentID
       ) xx    -- <<< this is the alias. (Don't forget this)
WHERE Faculty.DepartmentID= 'Department' AND Salary= 'MinSalary'
ORDER BY DepartmentID

xx是上例中子查询的名称。 (我觉得查询没有给你想要的结果

修改您的查询以获得更好的效果,(假设您因MYSQL功能而使用CONCAT

SELECT  CONCAT(a.FName,' ',a.LName) AS FacultyName,
        a.DepartmentID,
        a.Salary
FROM   Faculty a
        INNER JOIN
       (
        SELECT DepartmentID , 
               MIN(Salary) AS MinSalary
        FROM Faculty 
        GROUP BY DepartmentID
       ) xx    -- <<< this is the alias.
        ON  a.DepartmentID = xx.DepartmentID AND
            a.Salary = xx.MinSalary
-- WHERE .. (add extra condition here)
ORDER BY DepartmentID

SQLFiddle Demo

更新1

也可能是SQL Server 2012。 (已经支持CONCAT()

SELECT CONCAT([FName], ' ',[LName]) FullName, [DepartmentID], [Salary]
FROM
  (
    SELECT [FName], [LName], [DepartmentID], [Salary],
            ROW_NUMBER() OVER (partition BY DepartmentID
                              ORDER BY Salary) rn
    FROM Faculty
  ) x
WHERE x.rn = 1

SQLFiddle Demo

答案 1 :(得分:2)

作为标题提示中的错误消息,标准SQL要求FROM子句中的子查询具有名称。您还应该学习使用JOIN表示法,而不是逗号分隔的表名列表。您需要知道旧的(pre-SQL92)表示法才能识别它;你不应该自己使用它。

SELECT CONCAT(F.FName, ' ', F.LName) AS 'Faculty', F.DepartmentID, F.Salary
  FROM Faculty AS F
  JOIN (SELECT DISTINCT DepartmentID AS 'Department', MIN(Salary) AS 'MinSalary'
          FROM Faculty
         GROUP BY Department) AS D
    ON F.DepartmentID = D.Department AND F.Salary = D.MinSalary
 ORDER BY F.DepartmentID;

答案 2 :(得分:0)

您的派生表需要一个名称,这是错误消息的抱怨(请参阅下面的YouNeedToPutATableAliasHere):

SELECT CONCAT(FName,' ',LName) AS Faculty,DepartmentID,Salary
FROM Faculty,
(SELECT DISTINCT DepartmentID AS Department, MIN(Salary) AS MinSalary
FROM Faculty GROUP BY DepartmentID) AS YouNeedToPutATableAliasHere
WHERE Faculty.DepartmentID= 'Department' AND Salary= MinSalary
ORDER BY DepartmentID

注意我还删除了一些对我没有意义的单引号。 (也许你的意思是双引号,方括号或列名的反引号,但单引号可能不起作用 - 你可能需要为'部门'做同样的事情吗?)


除非指定关联两个表的条​​件,否则您还将创建笛卡尔连接。对“部门'Department'中最低工资的教员”的更好查询可能是(注意我已经删除了多余的GROUP BY):

SELECT CONCAT(FName,' ',LName) AS Faculty,F.DepartmentID,F.Salary
FROM Faculty F
INNER JOIN (
    SELECT DepartmentID, 
    MIN(Salary) AS MinSalary
    FROM Faculty 
    GROUP BY DepartmentID
) AS MinSal ON MinSal.DepartmentID = F.DepartmentID 
            AND F.Salary = MinSal.MinSalary
WHERE F.DepartmentID= 'Department'
ORDER BY F.DepartmentID

或者,由于您只选择了一个部门'Department',您可以这样做:

SELECT CONCAT(FName,' ',LName) AS Faculty,F.DepartmentID,F.Salary
FROM Faculty F
WHERE F.DepartmentID= 'Department'
AND F.Salary = (
    SELECT MIN(Salary)
    FROM Faculty 
    WHERE DepartmentID = F.DepartmentID
)

或者甚至(这也适用于多个部门,但可能比连接慢):

SELECT CONCAT(FName,' ',LName) AS Faculty,F.DepartmentID,F.Salary
FROM Faculty F
WHERE F.DepartmentID= 'Department'
AND NOT EXISTS(
    SELECT 1
    FROM Faculty FF
    WHERE FF.DepartmentID = F.DepartmentID
    AND FF.Salary < F.Salary
)

事实上,在阅读你的原始查询并努力在阅读@ RichardTheKiwi的评论时完成这个问题时,我会将这种解释纳入这个答案。这假设您正在使用MySQL(来自CONCAT函数)并且您的意思是反引号而不是列名的前向标记。它还假设您希望每个部门的最低薪酬教师,而不仅仅是指定的部门为“部门”。它只需要从我的第一个建议的查询中进行更改 - 即删除WHERE子句:

SELECT CONCAT(FName,' ',LName) AS Faculty,F.DepartmentID,F.Salary
FROM Faculty F
INNER JOIN (
    SELECT DepartmentID, 
    MIN(Salary) AS MinSalary
    FROM Faculty 
    GROUP BY DepartmentID
) AS MinSal ON MinSal.DepartmentID = F.DepartmentID 
            AND F.Salary = MinSal.MinSalary
ORDER BY F.DepartmentID

答案 3 :(得分:0)

  SELECT CONCAT(F.FName,' ',F.LName) AS Faculty, F.DepartmentID, F.Salary
    FROM Faculty F
    JOIN (
            SELECT DepartmentID,
                   MIN(Salary) AS MinSalary
              FROM Faculty
          GROUP BY DepartmentID
         ) G
      ON F.DepartmentID = G.DepartmentID AND F.Salary = G.MinSalary
ORDER BY F.DepartmentID

在别名中使用单引号的问题在于,您会陷入查询中明显的陷阱 - 您的第一个WHERE条件与DepartmentID与string字面'部门'相匹配即可。这也适用于'MinSalary'列。

值得注意的是,GROUP BY不需要DISTINCT - 因为您已经获得了DepartmentID的不同值。

对于第一个带到这里的问题 - 每个派生表(也就是子查询)都需要一个别名,或者用来识别它的列的东西。在下面的示例中,您可以看到ON子句的第一部分比较了两个DepartmentID列。别名FG在这里必须区分两者,因此需要对每个表进行别名。

答案 4 :(得分:0)

您的查询需要子查询的别名

select
    concat(F.FName, ' ', F.LName) as Faculty,
    F.DepartmentID,
    D.MinSalary
from faculty as F
    inner join
    (
        select T.DepartmentID, min(T.Salary) as MinSalary
        from Faculty as T
        group by T.DepartmentID
    ) as D on D.DepartmentID = F.DepartmentID and D.MinSalary = F.Salary
order by F.DepartmentID

答案 5 :(得分:0)

最好为表和子选择查询表使用别名:

    SELECT CONCAT(F.FName,' ',F.LName) AS Faculty,F.DepartmentID, F.Salary
    FROM Faculty F,
     (SELECT DISTINCT DepartmentID AS Department, MIN(Salary) AS MinSalary
      FROM Faculty GROUP BY DepartmentID) AS DEP_MIN_SALARY
    WHERE F.DepartmentID= DEP_MIN_SALARY.Department 
          AND F.Salary= DEP_MIN_SALARY.MinSalary
    ORDER BY FDepartmentID;

一个观察结果:您没有从派生表中检索任何列。希望这是故意的。