1

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

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
4

6 回答 6

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 演示

更新 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 演示

于 2012-10-27T05:03:10.077 回答
2

正如标题提示的错误消息,标准 SQL 要求 FROM 子句中的子查询具有名称。您还应该学习使用 JOIN 表示法,而不是逗号分隔的表名列表。您需要知道旧的(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;
于 2012-10-27T05:06:02.827 回答
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
于 2012-10-27T05:01:27.477 回答
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

在别名周围使用单引号的问题是您陷入了查询中明显的陷阱 - 您的第一个条件是将 DepartmentID 与文字'Department'WHERE匹配。这也适用于“MinSalary”列。string

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

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

于 2012-10-27T05:04:16.723 回答
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
于 2012-10-27T05:05:27.910 回答
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;

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

于 2012-10-27T05:05:45.257 回答