mysql-根据起始日期和日期连接两个表

mysql-根据起始日期和日期连接两个表,mysql,sql,Mysql,Sql,我有下面的表结构 表小时费率 CREATE TABLE `hour_rate` ( `hour_rate_id` int(11) NOT NULL AUTO_INCREMENT, `hour_rate` decimal(8,2) NOT NULL, `from_date` date NOT NULL, `employee_id` int(11) NOT NULL, PRIMARY KEY (`hour_rate_id`), UNIQUE KEY `idx-unique-hour_ra

我有下面的表结构

小时费率

CREATE TABLE `hour_rate` (
 `hour_rate_id` int(11) NOT NULL AUTO_INCREMENT,
 `hour_rate` decimal(8,2) NOT NULL,
 `from_date` date NOT NULL,
 `employee_id` int(11) NOT NULL,
 PRIMARY KEY (`hour_rate_id`),
 UNIQUE KEY `idx-unique-hour_rate-from_date-employee_id`
     (`from_date`,`employee_id`),
 KEY `idx-hour_rate-employee_id` (`employee_id`),
 CONSTRAINT `fk-hour_rate-employee_id` FOREIGN KEY (`employee_id`) 
     REFERENCES `employee` (`employee_id`) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE=InnoDB;
+-----------+------------+-------------+
| hour_rate | from_date  | employee_id |
+-----------+------------+-------------+
|     11.00 | 2018-01-10 |           1 |
|     12.00 | 2018-01-14 |           1 |
|     13.00 | 2018-01-18 |           1 |
|      5.00 | 2018-01-01 |           1 |
|     10.00 | 2018-01-15 |           2 |
+-----------+------------+-------------+
员工工作

CREATE TABLE `employee_work` (
 `employee_work_id` int(11) NOT NULL AUTO_INCREMENT,
 `project_id` int(11) NOT NULL,
 `employee_id` int(11) NOT NULL,
 `date` date NOT NULL,
 `hours` int(11) NOT NULL,
 PRIMARY KEY (`employee_work_id`),
 UNIQUE KEY `idx-unique-employee_work-employee_id-date` 
     (`employee_id`,`date`),
 KEY `idx-employee_work-employee_id` (`employee_id`),
 CONSTRAINT `fk-employee_work-employee_id` FOREIGN KEY (`employee_id`) 
     REFERENCES `employee` (`employee_id`) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE=InnoDB;
+-------------+------------+-------+
| employee_id | date       | hours |
+-------------+------------+-------+
|           1 | 2018-01-01 |     8 |
|           1 | 2018-01-02 |     8 |
|           1 | 2018-01-03 |     8 |
|           1 | 2018-01-04 |     8 |
|           1 | 2018-01-05 |     8 |
|           1 | 2018-01-08 |     8 |
|           1 | 2018-01-09 |     8 |
|           1 | 2018-01-10 |     8 |
|           1 | 2018-01-11 |     8 |
|           1 | 2018-01-12 |     8 |
|           1 | 2018-01-15 |     8 |
|           1 | 2018-01-16 |     8 |
|           1 | 2018-01-17 |     8 |
|           1 | 2018-01-18 |     8 |
|           1 | 2018-01-19 |     8 |
+-------------+------------+-------+
hour\u rate
包含从日期(
from\u date
)开始的员工及其小时费率(
hour\u rate
)的记录

employee\u work
包含员工每天工作时间的记录

我想根据
date
from\u date
,从
hour\u rate
中选择所有记录
employee\u work
,选择适当小时率的
hour\u rate
*
hours
,这样我就可以计算员工的工资了

例如,我有以下记录 在
小时费率中

CREATE TABLE `hour_rate` (
 `hour_rate_id` int(11) NOT NULL AUTO_INCREMENT,
 `hour_rate` decimal(8,2) NOT NULL,
 `from_date` date NOT NULL,
 `employee_id` int(11) NOT NULL,
 PRIMARY KEY (`hour_rate_id`),
 UNIQUE KEY `idx-unique-hour_rate-from_date-employee_id`
     (`from_date`,`employee_id`),
 KEY `idx-hour_rate-employee_id` (`employee_id`),
 CONSTRAINT `fk-hour_rate-employee_id` FOREIGN KEY (`employee_id`) 
     REFERENCES `employee` (`employee_id`) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE=InnoDB;
+-----------+------------+-------------+
| hour_rate | from_date  | employee_id |
+-----------+------------+-------------+
|     11.00 | 2018-01-10 |           1 |
|     12.00 | 2018-01-14 |           1 |
|     13.00 | 2018-01-18 |           1 |
|      5.00 | 2018-01-01 |           1 |
|     10.00 | 2018-01-15 |           2 |
+-----------+------------+-------------+
员工工作中

CREATE TABLE `employee_work` (
 `employee_work_id` int(11) NOT NULL AUTO_INCREMENT,
 `project_id` int(11) NOT NULL,
 `employee_id` int(11) NOT NULL,
 `date` date NOT NULL,
 `hours` int(11) NOT NULL,
 PRIMARY KEY (`employee_work_id`),
 UNIQUE KEY `idx-unique-employee_work-employee_id-date` 
     (`employee_id`,`date`),
 KEY `idx-employee_work-employee_id` (`employee_id`),
 CONSTRAINT `fk-employee_work-employee_id` FOREIGN KEY (`employee_id`) 
     REFERENCES `employee` (`employee_id`) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE=InnoDB;
+-------------+------------+-------+
| employee_id | date       | hours |
+-------------+------------+-------+
|           1 | 2018-01-01 |     8 |
|           1 | 2018-01-02 |     8 |
|           1 | 2018-01-03 |     8 |
|           1 | 2018-01-04 |     8 |
|           1 | 2018-01-05 |     8 |
|           1 | 2018-01-08 |     8 |
|           1 | 2018-01-09 |     8 |
|           1 | 2018-01-10 |     8 |
|           1 | 2018-01-11 |     8 |
|           1 | 2018-01-12 |     8 |
|           1 | 2018-01-15 |     8 |
|           1 | 2018-01-16 |     8 |
|           1 | 2018-01-17 |     8 |
|           1 | 2018-01-18 |     8 |
|           1 | 2018-01-19 |     8 |
+-------------+------------+-------+
我期望产生以下结果

+-------------+------------+-------+--------+
| employee_id | date       | hours | payment|
+-------------+------------+-------+--------+
|           1 | 2018-01-01 |     8 |    40.0|
|           1 | 2018-01-02 |     8 |    40.0|
|           1 | 2018-01-03 |     8 |    40.0|
|           1 | 2018-01-04 |     8 |    40.0|
|           1 | 2018-01-05 |     8 |    40.0|
|           1 | 2018-01-08 |     8 |    40.0|
|           1 | 2018-01-09 |     8 |    40.0|
|           1 | 2018-01-10 |     8 |    88.0|
|           1 | 2018-01-11 |     8 |    88.0|
|           1 | 2018-01-12 |     8 |    88.0|
|           1 | 2018-01-15 |     8 |    96.0|
|           1 | 2018-01-16 |     8 |    96.0|
|           1 | 2018-01-17 |     8 |    96.0|
|           1 | 2018-01-18 |     8 |   104.0|
|           1 | 2018-01-19 |     8 |   104.0|
+-------------+------------+-------+--------+

一种方法使用相关子查询来获取速率:

select ew.*,
       (ew.hours *
        (select hr.hour_rate
         from hour_rate hr
         where hr.employee_id = ew.employee_id and
               hr.from_date >= ew.date
         order by hr.from_date
         limit 1
        )
       ) as daily_pay
from employee_work ew;

您可以通过在两个表中使用select来完成此操作。您的数据示例如下:

select ew.employee_id, ew.date, ew.hours, hr.employee_id, hr.from_date (hr.hour_rate * ew.hours) as payment

from employee_work ew, hour_rate hr 

where ew.employee_id = hr.employee_id

您没有适用于2018-01-01至2018-01-09的起始日期,您是在应用默认值还是只是在示例中没有显示?@P.Salmon第四行是费率。输入的日期不是cosecuriveeew.date=hr.from\u date?并不是每一次约会都有收费。