简体   繁体   English

SQL中的SQL连接和子查询

[英]SQL join and sub query in sql

I have a table employee having columns id (primary key), *employee_name* and another table called employee_works with columns *employee_id* (foreign key referencing employee.id), *start_date* (datetime), *finish_date* (datetime). 我有一个表employee它具有列id (主键),* employee_name *和另一个名为employee_works表,该表具有* employee_id *(引用employee.id的外键),* start_date *(日期时间),* finish_date *(日期时间)列。

Here are some datas for employee table: 以下是有关employee表的一些数据:

 **id**   **employee_name**
 1      employee A
 2      employee B
 3      employee C
 4      employee D
 5      employee E
 6      employee F
 7      employee G

employee_works table: employee_works表:

1  2010-01-01 00:00:00   NULL
2  2010-01-01 00:00:00   2010-01-10 10:00:00"
2  2010-01-13 00:00:00   2010-01-15 10:00:00"
2  2010-01-31 00:00:00   NULL
4  2010-02-18 00:00:00   2011-01-31 00:00:00"
6  2010-02-18 00:00:00   NULL

NULL value means the employee still works. NULL值表示员工仍在工作。 I need to get a single query showing the list of persons in employee, if they worked with us, who still works in our company, who left and if possible, for how long they worked with us. 我需要得到一个查询,以显示员工中的人员列表(如果他们与我们一起工作,仍在我们公司工作),离职人员(如果可能)以及与我们一起工作了多长时间。 Example: 例:

id     employee_name       status
1      Employee A       Still with us
3      Employee C       Never worked
4      Employee D       Left

My attempt: 我的尝试:

SELECT emp.id,emp.name,
CASE
WHEN occ.finish_date is NULL and occ.start_date is NOT NULL THEN 'Still working'
WHEN occ.finish_date is NULL and occ.start_date is NULL THEN 'Never Worked'
WHEN occ.finish_date is NOT  NULL and occ.start_date is NOT NULL THEN 'Left'
END
AS status

FROM employee AS emp
LEFT JOIN employee_works AS occ ON emp.id=occ.employee_id 
GROUP BY emp.id, occ.finish_date

I also want to get the total no of days the employees have worked in another column? 我还想获得员工在另一列中工作的总天数吗?

The problem is that you have a group by but no aggregations for the definition of status. 问题是您有一个分组依据,但没有用于状态定义的汇总。 Mysql does not give you a syntax error. Mysql没有给您语法错误。 Instead, it gives you a random status: 相反,它为您提供了随机状态:

Try something like this instead: 尝试这样的事情:

select id, name,
       (CASE WHEN statusint = 3
             THEN 'Still working'
             WHEN statusint = 1 or statusint is null
             THEN 'Never Worked'
             WHEN statusint = 2
             THEN 'Left'
        END) AS status,
       days_worked
from (SELECT emp.id, emp.name,
             max(CASE WHEN occ.departure_date is NULL and occ.start_date is NOT NULL
                      THEN 3
                      WHEN occ.departure_date is NULL and occ.start_date is NULL
                      THEN 1
                      WHEN occ.departure_date is NOT NULL and occ.start_date is NOT NULL
                      THEN 2
                 END) AS statusint,
             sum(datediff(coalesce(departure_date, curdate()), occ.start_date
                ) as days_worked
      FROM employee emp LEFT JOIN
           employee_works occ
           ON emp.id=occ.employee_id
      GROUP BY emp.id, emp.name
     ) eg

This "feature" of mysql is called hidden columns. mysql的这种“功能”称为隐藏列。 Folks who write mysql (and many who use it) think this is a great feature. 编写mysql的人们(以及许多使用mysql的人们)认为这是一个很棒的功能。 Many people who use other databases just scratch their heads and wonder why any database would act so strangely. 许多使用其他数据库的人只是scratch之以鼻,想知道为什么任何数据库的行为都会如此奇怪。

By the way, you should check if someone who is employeed multiple times gets assigned a new id. 顺便说一句,您应该检查是否多次雇用的人被分配了新的ID。 If so, your query might need more advanced name matching methods. 如果是这样,您的查询可能需要更高级的名称匹配方法。

Try to simplify your condition. 尝试简化您的状况。

SELECT  a.*, 
        CASE
            WHEN b.employeeID IS NULL THEN 'NEVER WORKED'
            WHEN b.finish_date IS NULL THEN 'STILL WORKING'
            WHEN DATE(b.finish_date) < CURDATE() THEN 'LEFT'
        END as `Status`
FROM    employee a
            LEFT JOIN employee_works b
                on a.id = b.employeeID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM