mysql - 嵌套查询的问题

标签 mysql sqlite

嘿伙计们,我一直在研究这个问题,但我无法解决它。有人可以帮我解决这个问题吗?我尝试了很多方法但无法解决它,任何人都可以通过使用嵌套查询并仅使用(GROUP BY、HAVING、SELECT、FROM 和 WHERE)来帮助我

(Qs)You would like to know the departments with more than 5 full-time young employees (Age <25) each department has. Calculate the average salary of the employees who work for those departments. This newly calculated field should be named as "AvgSalary" and be returned by your query.

这是其架构。

Employee(EmployeeID: Integer, EmpName: String, Age: Integer, Salary: Real)  
Department(DeptID: Integer, DeptName: String, Budget: Real, ManagerID: Integer)
Works(EmployeeID: Integer, DeptID: Integer, Percent_Time: Integer)

最佳答案

如果您只对目标群体(25 岁以下)的平均工资感兴趣,则不需要嵌套...

SELECT d.*
     , AVG(********) AvgSalary 
  FROM ******** 
  JOIN ********
    ON ******** = ******** 
  JOIN ********  
    ON ******** = ******** 
 WHERE ******** = 100 
   AND ******** < 25 
 GROUP 
    BY ******** 
HAVING COUNT(*) > 5; 

如果您需要目标组返回的部门内所有员工的平均工资,那么您可以使用子查询,但我会使用 CASE 语句,

关于mysql - 嵌套查询的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30418080/

相关文章:

php - SQLSTATE[HY000] : General error: 1 table posts has no column named *

android - 无法使用 Sqlite AssetHelper 库打开预安装的数据库

c# - 具有多线程访问的SQLitePCLRaw.provider.e_sqlite3.dll中的System.AccessViolationException或System.ExecutionEngineException崩溃

php - Json 到 Html 使用 bootbox?

php - 无法在 Laravel 中迁移文件

java - 使用自定义对象填充微调器

c# - SQLite 数据库数据未复制到 Windows 8.1 Store App 中的 LocalFolder

mysql查询需要不同的结构

javascript - jquery ui slider 发布到mysql

mysql - 仅将唯一条目从一个表复制到另一个 MySQL