4张表怎么查询

wangcb80 2014-06-21 08:46:32
有如下表:
create table 中心(
中心编号 int(11) not null primary key,
中心名称 varchar(50) not null
)character set utf8;

create table 部门(
部门编号 int(11) not null primary key,
部门名称 varchar(50) not null,
所属中心 int(11) not null
)character set utf8;

create table 被考核员工
员工编号 int(11) not null primary key,
员工姓名 varchar(50) not null,
所属部门 int(11) not null
)character set utf8;

create table 违规记录(
编号 int(11) not null auto_increment primary key,
员工编号 int(11) not null,
考核月份 int(11) not null,
奇次打卡结算次数 int(11),
奇次扣款数 int(11),
迟到(或早退)次数 int(11),
迟到(或早退)分钟数 int(11),
迟到(或早退)扣款数 int(11),
未出勤结算天数 int(11),
未出勤扣款数 int(11),
本月合计扣款数 int(11) not null
)character set utf8;

我现在查询出了如下的结果(所有有违规记录的员工记录):
select
员工姓名,
sum(迟到(或早退)次数) as 迟到次数,
sum(迟到(或早退)分钟数) as 总迟到分钟数,
sum(迟到(或早退)扣款数) as 迟到总扣款数,
sum(奇次打卡结算次数) as 奇次打卡数,
sum(奇次扣款数) as 奇次扣款数,
sum(未出勤结算天数) as 未出勤天数,
sum(未出勤扣款数) as 未出勤扣款数,
sum(本月合计扣款数) as 合计扣款数
from
违规记录,
被考核员工
where
被考核员工.员工编号 = 违规记录.员工编号
group by 员工姓名
order by 合计扣款数 desc;

问:怎么查询出所有被考核人员的情况(包括有违规和没有违规的),并且在结果中显示出这个员工所属部门和所属中心名称。
...全文
107 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2014-06-21
  • 打赏
  • 举报
回复
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。 参考一下这个贴子的提问方式http://bbs.csdn.net/topics/320211382 1. 你的 create table xxx .. 语句 2. 你的 insert into xxx ... 语句 3. 结果是什么样,(并给以简单的算法描述) 4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL) 这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
ayzen1988 2014-06-21
  • 打赏
  • 举报
回复


select x.*,(case when y.迟到次数 is null then 0 else y.迟到次数)迟到次数 end from
(select * from 被考核员工 a,部门 b,中心 c where a.所属部门 = b.部门编号 and b.所属中心 = c.中心编号)x
left join
(select 员工编号,sum(迟到(或早退)次数) as 迟到次数 from 违规记录 group by 员工编号)y
on x.员工编号 = y.员工编号

//其他情况按照  迟到次数  以此类推、就不全部写出来了

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧