我如何在MYSQL中创建这个SELECT查询?

时间:2016-09-09 02:01:11

标签: mysql sql select

我有两个Z表。这些被命名为: MYSQLattendance_summary。两个表都有相同的字段,但可能有不同的数据。

这是两张表中2016年8月的结果。

attendance_summary_cecb

使用这两个表,在一个查询中,我需要获取

mysql> SELECT emp_id, date, days, ot_days, ot_hours FROM attendance_summary;
+--------+------------+------+---------+----------+
| emp_id | date       | days | ot_days | ot_hours |
+--------+------------+------+---------+----------+
|      4 | 2016-08-01 |   10 |       0 |     20.0 |
|      4 | 2016-08-16 |   13 |       0 |     14.5 |
|     12 | 2016-08-01 |   12 |       0 |     10.0 |
|     12 | 2016-08-16 |   11 |       2 |     16.5 |
|     14 | 2016-08-01 |   12 |       2 |     15.0 |
|     14 | 2016-08-16 |   10 |       0 |     16.0 |
|     15 | 2016-08-01 |   11 |       0 |     16.0 |
|     15 | 2016-08-16 |    9 |       3 |     21.0 |
+--------+------------+------+---------+----------+
8 rows in set (0.00 sec)

mysql> SELECT emp_id, date, days, ot_days, ot_hours FROM attendance_summary_cecb;
+--------+------------+------+---------+----------+
| emp_id | date       | days | ot_days | ot_hours |
+--------+------------+------+---------+----------+
|      4 | 2016-08-01 |   15 |       0 |     20.0 |
|      4 | 2016-08-16 |   10 |       0 |     12.0 |
|     12 | 2016-08-01 |   12 |       1 |     10.0 |
|     12 | 2016-08-16 |   11 |       2 |     16.5 |
|     14 | 2016-08-01 |   12 |       2 |     15.0 |
|     14 | 2016-08-16 |   10 |       0 |     16.0 |
|     15 | 2016-08-01 |   10 |       0 |     16.0 |
|     15 | 2016-08-16 |    9 |       0 |     21.0 |
+--------+------------+------+---------+----------+
8 rows in set (0.00 sec)

这是我尝试的方式:

> From `attendance_summary` 
>  - Total Days
>  - Total OT Days
>  - Total OT Hours 
> 
> From `attendance_summary_cecb` 
>  - Total Days
>  - Total OT Days
>  - Total OT Hours

更新 这是这两个表的表结构:

SELECT e.emp_id
     , e.full_name
     , MAX(days1) as days1
     , MAX(ot_days1) as ot_days1
     , MAX(ot_hours1) as ot_hours1
     , MAX(days2) as days2
     , MAX(ot_days2) as ot_days2
     , MAX(ot_hours2) as ot_hours2
     , SUM(days1+days2) AS olekma_days
     , SUM(ot_days1+ot_days2) AS olekma_ot_days
     , SUM(ot_hours1+ot_hours2) AS olekma_ot_hours       
     , SUM(days3+days4) AS cecb_days
     , SUM(ot_days3+ot_days4) AS cecb_ot_days
     , SUM(ot_hours3+ot_hours4) AS cecb_ot_hours        
FROM 
       (
        SELECT emp_id
           , CASE DAY(date) WHEN 1 THEN days ELSE 0 END as days1
           , CASE DAY(date) WHEN 1 THEN ot_days ELSE 0 END as ot_days1
           , CASE DAY(date) WHEN 1 THEN ot_hours ELSE 0 END as ot_hours1
           , CASE DAY(date) WHEN 16 THEN days ELSE 0 END as days2
           , CASE DAY(date) WHEN 16 THEN ot_days ELSE 0 END as ot_days2
           , CASE DAY(date) WHEN 16 THEN ot_hours ELSE 0 END as ot_hours2
        FROM attendance_summary
        WHERE MONTH(date) = 08 AND YEAR(date) = 2016

        UNION

        SELECT emp_id
         , CASE DAY(date) WHEN 1 THEN days ELSE 0 END as days3
         , CASE DAY(date) WHEN 1 THEN ot_days ELSE 0 END as ot_days3
         , CASE DAY(date) WHEN 1 THEN ot_hours ELSE 0 END as ot_hours3
         , CASE DAY(date) WHEN 16 THEN days ELSE 0 END as days4
         , CASE DAY(date) WHEN 16 THEN ot_days ELSE 0 END as ot_days4
         , CASE DAY(date) WHEN 16 THEN ot_hours ELSE 0 END as ot_hours4
        FROM attendance_summary_cecb
        WHERE MONTH(date) = 08 AND YEAR(date) = 2016
       ) as s
LEFT JOIN employees e USING (emp_id)
INNER JOIN categories c ON c.id = e.category_id AND c.salary_type = 'D'
WHERE e.category_id = 6
GROUP BY e.emp_id\G

但它对我不起作用。希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

你的问题的答案很简单。顺便说一下,我不得不从底部创建2个缺失的表来确认你的错误。

当你做

select col1, col2 from t1
union
select col9 as fred, col11 as john from t2

或该主题与scads表的任何变体,唯一返回的列名来自联盟的第一个成员。

换句话说,弗雷德和约翰没有出来。

因此,派生表s中没有您的days3和days4。

然后你需要重新考虑它,但这回答了问题:p

来自名为UNION Syntax的MySQL手册页:

  

第一个SELECT语句中的列名用作   返回结果的列名称。列出的选定列   每个SELECT语句的相应位置应该相同   数据类型。 (例如,第一列选择的第一列   语句应与选择的第一列具有相同的类型   其他陈述。)