将两个mysql查询与不平行列

时间:2017-03-30 05:11:33

标签: mysql

我有两个我想要结合的mysql查询。我羞耻地发布在这里,因为我已经尝试了很多组合来使其发挥作用,但无法弄明白。我想我的查询写作能力已达到上限。 :(

查询#1

        SELECT 
        time_clock.id AS clock_id, 
        employees.first_name, employees.last_name, employees.nick_name, employees.pay_schedule, time_clock.comments, 
        employees.id AS employee_id, time_clock.punch_in, 
        UNIX_TIMESTAMP(time_clock.punch_in) AS punch_in_unix, 
        UNIX_TIMESTAMP(time_clock.punch_out) AS punch_out_unix, 
        TIMESTAMPDIFF(MINUTE, time_clock.punch_in, time_clock.punch_out) AS time_spent
        FROM time_clock 
        JOIN employees ON employees.id = time_clock.employee_id 
        WHERE time_clock.id IN 
            (
            SELECT MAX(time_clock.id)
            FROM time_clock
            WHERE employees.pay_schedule='hourly'
            GROUP BY employee_id
            )
        ORDER BY last_name, first_name ASC

查询#2

        SELECT
        employees.first_name, employees.last_name,
        employees.id AS employee_id, 
        SUM(TIME_TO_SEC(time_clock.punch_out) - TIME_TO_SEC(time_clock.punch_in)) AS summed_seconds
        FROM time_clock 
        JOIN employees ON employees.id = time_clock.employee_id 
        WHERE YEAR(punch_in + interval 4 day) = YEAR(CURDATE()) AND
        WEEK(punch_in + interval 4 day) = WEEK(CURDATE())
        GROUP BY employee_id

每个查询都可以自行运行。第一个查询为每个员工选择最新的时间事件。第二个查询总结了上一个支付期间每个员工的记录时间(每周一次,从周三到周二)。

我希望有两个查询同时执行:为每位员工选择最新的时间事件,并为上次支付期间记录的时间总计时间。 (我这样做是为了提供一个界面来跟踪哪些员工加班。)

我能得到的任何帮助都会很棒。我也一直在探索将结果作为数组在php中结合起来得到我想要的东西,但这给我带来了一系列问题,我怀疑它并不高效。

Query#1的输出如下:

enter image description here

查询#2的输出如下所示:

enter image description here

这基本上是我希望能够拥有的输出:

enter image description here

感谢您提供的任何帮助。我的前额比我敲打它的桌子更快地凹陷。

-Matt

PS - 我尝试将它们结合起来的尝试之一如下。它似乎在我的所有尝试中产生了最明智的错误("使用的SELECT语句具有不同数量的列"),所以我不知道它到底有多接近。

SELECT 
        time_clock.id AS clock_id, 
        employees.first_name, employees.last_name, employees.nick_name, employees.pay_schedule, time_clock.comments, 
        employees.id AS employee_id, time_clock.punch_in, 
        UNIX_TIMESTAMP(time_clock.punch_in) AS punch_in_unix, 
        UNIX_TIMESTAMP(time_clock.punch_out) AS punch_out_unix, 
        TIMESTAMPDIFF(MINUTE, time_clock.punch_in, time_clock.punch_out) AS time_spent
        FROM (
            SELECT *
            FROM time_clock 
            LEFT JOIN employees ON employees.id = time_clock.employee_id
            WHERE time_clock.id IN 
                (
                SELECT MAX(time_clock.id)
                FROM time_clock
                WHERE employees.pay_schedule='hourly'
                GROUP BY employee_id
                )

            UNION ALL

            SELECT
                employees.first_name, employees.last_name,
                employees.id AS employee_id, 
                SUM(TIME_TO_SEC(time_clock.punch_out) - TIME_TO_SEC(time_clock.punch_in)) AS summed_seconds
                FROM time_clock 
                LEFT JOIN employees ON employees.id = time_clock.employee_id 
                WHERE YEAR(punch_in + interval 4 day) = YEAR(CURDATE()) AND
                      WEEK(punch_in + interval 4 day) = WEEK(CURDATE())
                GROUP BY employee_id
                ) as thing_one
        JOIN employees ON employees.id = time_clock.employee_id 
        ORDER BY last_name, first_name ASC

2 个答案:

答案 0 :(得分:0)

你可以尝试

SELECT     time_clock.id AS clock_id, 
           employee_lastest_event.first_name, 
           employee_lastest_event.last_name, 
           employee_lastest_event.nick_name, 
           employee_lastest_event.pay_schedule, 
           time_clock.comments, 
           time_clock.employee_id, 
           time_clock.punch_in, 
           UNIX_TIMESTAMP(time_clock.punch_in)                              AS punch_in_unix,
           UNIX_TIMESTAMP(time_clock.punch_out)                             AS punch_out_unix,
           TIMESTAMPDIFF(minute, time_clock.punch_in, time_clock.punch_out) AS time_spent, 
           COALESCE(sum_period.summed_seconds, 0)                           AS summed_seconds 
FROM       time_clock 
           INNER JOIN 
            ( 
              SELECT     time_clock.employee_id, 
                         employees.first_name, 
                         employees.last_name, 
                         employees.nick_name, 
                         employees.pay_schedule, 
                         MAX(time_clock.id) AS clock_id 
              FROM       time_clock 
              INNER JOIN employees 
              ON         employees.id = time_clock.employee_id 
              WHERE      employees.pay_schedule='hourly' 
              GROUP BY   time_clock.employee_id ) employee_lastest_event 
           ON  (employee_lastest_event.clock_id = time_clock.id) 
           LEFT JOIN 
           ( 
              SELECT   time_clock.employee_id, Sum(time_to_sec(time_clock.punch_out) - time_to_sec(time_clock.punch_in)) AS summed_seconds
              FROM     time_clock 
              WHERE    year(time_clock.punch_in + INTERVAL 4 day) = year(curdate()) 
              AND      week(time_clock.punch_in + INTERVAL 4 day) = week(curdate()) 
              GROUP BY time_clock.employee_id ) sum_period 
            ON (sum_period.employee_id = time_clock.employee_id)
ORDER BY last_name, first_name ASC  

答案 1 :(得分:0)

只需使用employee_id

加入两个查询
SELECT clock_id,
table1.first_name,
table1.last_name,
nick_name,
pay_schedule,
comments,
table1.employee_id,
punch_in,
punch_in_unix,
punch_out_unix,
time_spent,
summed_seconds
(SELECT 
        time_clock.id AS clock_id, 
        employees.first_name, employees.last_name, employees.nick_name, employees.pay_schedule, time_clock.comments, 
        employees.id AS employee_id, time_clock.punch_in, 
        UNIX_TIMESTAMP(time_clock.punch_in) AS punch_in_unix, 
        UNIX_TIMESTAMP(time_clock.punch_out) AS punch_out_unix, 
        TIMESTAMPDIFF(MINUTE, time_clock.punch_in, time_clock.punch_out) AS time_spent
        FROM time_clock 
        JOIN employees ON employees.id = time_clock.employee_id 
        WHERE time_clock.id IN 
            (
            SELECT MAX(time_clock.id)
            FROM time_clock
            WHERE employees.pay_schedule='hourly'
            GROUP BY employee_id
            )) as table1,

(SELECT
        employees.first_name, employees.last_name,
        employees.id AS employee_id, 
        SUM(TIME_TO_SEC(time_clock.punch_out) - TIME_TO_SEC(time_clock.punch_in)) AS summed_seconds
        FROM time_clock 
        JOIN employees ON employees.id = time_clock.employee_id 
        WHERE YEAR(punch_in + interval 4 day) = YEAR(CURDATE()) AND
        WEEK(punch_in + interval 4 day) = WEEK(CURDATE())
        GROUP BY employee_id
) as table2
WHERE table1.employee_id = table2.employee_id