如何计算夜班和周日的额外工资

发布于 2024-11-16 01:09:01 字数 1224 浏览 2 评论 0原文

我在 MySQL 中做了一个表,以日期时间类型存储工作班次的开始和结束时间。我需要从中计算出两件事(在 MySQL 中)。首先,晚上 10 点到早上 6 点之间某个班次工作了多少分钟,以及周日工作了多少分钟。周日应该优先于那些夜间时间,并且不应该将那些周日早上算两次。

先感谢您。

D.

顺便说一句,我尝试了这个,但没有成功:

SELECT *,
IF(DAYNAME(start)=DAYNAME(end),
    IF(TIME(start)<='6:00:00' AND TIME(end)<='6:00:00',TIME(end)-TIME(start),
        IF(TIME(start)>='22:00:00' AND TIME(end)>='22:00:00',TIME(end)-TIME(start),
            IF(TIME(start)>='6:00:00' AND TIME(end)<='22:00:00',0,
                IF(TIME(start)<='6:00:00' AND TIME(end)<='22:00:00','6:00:00'-TIME(start),
                    IF(TIME(start)>='6:00:00' AND TIME(end)>='22:00:00',TIME(end)-'22:00:00',NULL)
                    )
                )
            )
        ),
    IF(TIME(start)<='22:00:00' AND TIME(end)<='6:00:00','2:00:00'+TIME(end),
        IF(TIME(start)<='22:00' AND TIME(end)>='6:00:00','8:00:00',
            IF(TIME(start)>='22:00:00' AND TIME(end)<='6:00:00','24:00:00'-TIME(start)+TIME(end),
                IF(TIME(start)>='22:00:00' AND TIME(end)>='6:00:00','24:00:00'-TIME(start)+'6:00:00',NULL)
                )
            )
        )
    ) AS ExtraTime FROM `work`

I did a table in MySQL storing start and end times of working shifts in datetime type. I need to calculate two things from them (in MySQL). First of all, how many minutes someone has worked in given shift between 10pm and 6am, as well as how many minutes someone has worked on a sunday. Sunday should have priority over those night times, and it shouldn't count those sunday mornings twice.

Thank you in advance.

D.

I tried this btw, but it didn't work:

SELECT *,
IF(DAYNAME(start)=DAYNAME(end),
    IF(TIME(start)<='6:00:00' AND TIME(end)<='6:00:00',TIME(end)-TIME(start),
        IF(TIME(start)>='22:00:00' AND TIME(end)>='22:00:00',TIME(end)-TIME(start),
            IF(TIME(start)>='6:00:00' AND TIME(end)<='22:00:00',0,
                IF(TIME(start)<='6:00:00' AND TIME(end)<='22:00:00','6:00:00'-TIME(start),
                    IF(TIME(start)>='6:00:00' AND TIME(end)>='22:00:00',TIME(end)-'22:00:00',NULL)
                    )
                )
            )
        ),
    IF(TIME(start)<='22:00:00' AND TIME(end)<='6:00:00','2:00:00'+TIME(end),
        IF(TIME(start)<='22:00' AND TIME(end)>='6:00:00','8:00:00',
            IF(TIME(start)>='22:00:00' AND TIME(end)<='6:00:00','24:00:00'-TIME(start)+TIME(end),
                IF(TIME(start)>='22:00:00' AND TIME(end)>='6:00:00','24:00:00'-TIME(start)+'6:00:00',NULL)
                )
            )
        )
    ) AS ExtraTime FROM `work`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅听莫相离 2024-11-23 01:09:01

工作时间:

SELECT TIMESTAMPDIFF(MINUTE, `start_time`, `end_time`) FROM ... WHERE DAYOFWEEK(`start_time`) != 1

周日工作时间:

SELECT TIMESTAMPDIFF(MINUTE, `start_time`, `end_time`) FROM ... WHERE DAYOFWEEK(`start_time`) = 1

minutes worked:

SELECT TIMESTAMPDIFF(MINUTE, `start_time`, `end_time`) FROM ... WHERE DAYOFWEEK(`start_time`) != 1

minutes worked on Sunday:

SELECT TIMESTAMPDIFF(MINUTE, `start_time`, `end_time`) FROM ... WHERE DAYOFWEEK(`start_time`) = 1
余厌 2024-11-23 01:09:01

在 PHP 中,尝试以下代码(警告:尚未经过测试):

<?php
$regularPay = 100; //100 dollars a day
$weekendPay = $regularPay + ($regularPay / 2); //150 dollars over weekend
$link = mysqli_connect("localhost","username","password");
mysqli_select_db("dbname");
$pay = date("w")==0 or date("w")==6 ? $weekendPay : $regularPay; //0=Sun, 6=Sat
$query = mysqli_query($link,"INSERT INTO pay SET employee='John Smith',pay='$pay'");
?>

只需将字符串替换为适合您的计算机的值即可。

广告@m。

In PHP, try this code (warning: it hasn't been tested):

<?php
$regularPay = 100; //100 dollars a day
$weekendPay = $regularPay + ($regularPay / 2); //150 dollars over weekend
$link = mysqli_connect("localhost","username","password");
mysqli_select_db("dbname");
$pay = date("w")==0 or date("w")==6 ? $weekendPay : $regularPay; //0=Sun, 6=Sat
$query = mysqli_query($link,"INSERT INTO pay SET employee='John Smith',pay='$pay'");
?>

Simply replace the strings with the appropriate values for your computer.

Ad@m.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文