作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
This is my Database table
This is my front end where i want display data:
我需要的是基于休假类型和半天全天的员工休假记录。从左到右员工姓名然后年度计数基于 half day = 1 in db 和 leave type = annual 依此类推,最后显示 total 列和 half_day annual,exam 和 unpaid 中所有列的一些列乘以 0.5。
我尝试过的:
SELECT users.name, leave_type, count( leaves.id )
FROM leaves
INNER JOIN users ON users.id = leaves.employee
GROUP BY leaves.leave_type, users.name
SELECT leaves.*, count( users.id )
FROM leaves
LEFT JOIN users ON users.id=leaves.employee
GROUP BY users.name
在此先感谢,请帮忙。
最佳答案
好的,那么,让我们试试这个,看看它是否有效。
这是我掌握的架构。它非常简单,但支持您的用例。
CREATE TABLE employees (
id int unsigned auto_increment,
name varchar(255),
PRIMARY KEY(id)
);
CREATE TABLE leave_type (
id int unsigned auto_increment,
name varchar(255),
PRIMARY KEY(id)
);
CREATE TABLE leave_log (
id int unsigned auto_increment,
leave_type_id int unsigned,
employee_id int unsigned,
is_full_day int unsigned,
is_half_day int unsigned,
PRIMARY KEY(id)
);
一些测试数据...
INSERT INTO employees VALUES (14, 'Lisa'), (15, 'Homer'), (13, 'Bart');
INSERT INTO leave_type VALUES (1, 'Annual'), (2, 'Unpaid'), (3, 'Exam');
INSERT INTO leave_log VALUES (NULL, 3, 14, 1, 0), (NULL, 1, 14, 1, 0), (NULL, 1, 14, 0, 1), (NULL, 1, 14, 0, 1);
INSERT INTO leave_log VALUES (NULL, 2, 15, 0, 1);
INSERT INTO leave_log VALUES (NULL, 3, 13, 1, 0), (NULL, 1, 13, 1, 0);
不要过分关注列名称和定义,我根本没有完善架构,因为我对您的应用了解不够,无法做到这一点。
一旦创建了架构并在其中存储了数据,这个非常简单的查询就可以完成您想要的操作。
SELECT e.name, SUM(annual.is_half_day), SUM(unpaid.is_half_day), SUM(exam.is_half_day),
SUM(annual.is_full_day), SUM(unpaid.is_full_day), SUM(exam.is_full_day)
FROM employees e
LEFT JOIN leave_log annual ON annual.leave_type_id = 1 AND annual.employee_id = e.id
LEFT JOIN leave_log unpaid ON unpaid.leave_type_id = 2 AND unpaid.employee_id = e.id
LEFT JOIN leave_log exam ON exam.leave_type_id = 3 AND exam.employee_id = e.id
GROUP BY e.id
看看你的想法。这可能是一个高性能或重负载的应用程序吗?
编辑
此查询涉及更多,可能会有一些性能缺陷,但可能更准确。
SELECT e.name, e.id,
IFNULL(annual_half.total, 0) annual_half,
IFNULL(unpaid_half.total, 0) unpaid_half,
IFNULL(exam_half.total, 0) exam_half,
IFNULL(annual_full.total, 0) annual_full,
IFNULL(unpaid_full.total, 0) unpaid_full,
IFNULL(exam_full.total, 0) exam_full
FROM employees e
LEFT JOIN ( SELECT SUM(is_full_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_full_day = 1 AND leave_type_id = 1 GROUP BY 3, 2) annual_full ON annual_full.employee_id = e.id
LEFT JOIN ( SELECT SUM(is_full_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_full_day = 1 AND leave_type_id = 2 GROUP BY 3, 2) unpaid_full ON unpaid_full.employee_id = e.id
LEFT JOIN ( SELECT SUM(is_full_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_full_day = 1 AND leave_type_id = 3 GROUP BY 3, 2) exam_full ON exam_full.employee_id = e.id
LEFT JOIN ( SELECT SUM(is_half_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_half_day = 1 AND leave_type_id = 1 GROUP BY 3, 2) annual_half ON annual_half.employee_id = e.id
LEFT JOIN ( SELECT SUM(is_half_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_half_day = 1 AND leave_type_id = 2 GROUP BY 3, 2) unpaid_half ON unpaid_half.employee_id = e.id
LEFT JOIN ( SELECT SUM(is_half_day) as total, employee_id, leave_type_id FROM leave_log WHERE is_half_day = 1 AND leave_type_id = 3 GROUP BY 3, 2) exam_half ON exam_half.employee_id = e.id
GROUP BY 1;
关于php - 根据考试、无薪、年度等休假类型从 mysql 数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32136843/
我是一名优秀的程序员,十分优秀!