1

我有三张表:年份、员工、职位。假设我已经在这些表中拥有这些数据。

years:
----------------
| id | name    |
----------------
|  1 | 2011    |
----------------

positions:
------------------------------
| id | name        | year_id |
------------------------------
|  1 | Director    |  1      |
|  2 | Manager     |  1      |
------------------------------

employees:
---------------------------------------------------------
| id | name                     | position_id | year_id |
---------------------------------------------------------
|  1 | Employee A (Director)    |  1          |  1      |
|  2 | Employee B (Manager)     |  2          |  1      |
---------------------------------------------------------

=========================================

年表是一个中心点。如果我插入新的一年记录,我还必须复制与上一年相关的所有职位和员工

所以如果我将 2012 年插入到 years 表中,数据应该是这样的:

years:
----------------
| id | name    |
----------------
|  1 | 2011    |
|  2 | 2012    |
----------------

positions:
------------------------------
| id | name        | year_id |
------------------------------
|  1 | Director    |  1      |
|  2 | Manager     |  1      |
|  3 | Director    |  2      |
|  4 | Manager     |  2      |
------------------------------

employees:
---------------------------------------------------------
| id | name                     | position_id | year_id |
---------------------------------------------------------
|  1 | Employee A (Director)    |  1          |  1      |
|  2 | Employee B (Manager)     |  2          |  1      |
|  3 | Employee A (Director)    |  3   (?)    |  2      |
|  4 | Employee B (Manager)     |  4   (?)    |  2      |
---------------------------------------------------------

注意employees表格第三行和第四行的问号。

我使用这些查询来插入新的一年并复制所有相关的职位和员工:

// Insert new year record
INSERT INTO years (name) VALUES (2012);

// Get last inserted year ID
$inserted_year_id = .......... // skipped

// Copy positions
INSERT INTO positions (name, year_id) SELECT name, $inserted_year_id AS last_year_id FROM positions WHERE year_id = 1;

// Copy employees
INSERT INTO employees (name, position_id, year_id) SELECT name, position_id, $inserted_year_id AS last_year_id FROM employees WHERE year_id = 1;

问题在于复制员工查询。我找不到获取或跟踪新职位 ID 的方法。

有没有简单的方法来做到这一点?

非常感谢你。

4

2 回答 2

2

您的数据模型存在严重缺陷,可能需要彻底检修,但如果您坚持按照您的描述复制数据,这应该可以解决问题:

// Copy employees
INSERT INTO employees (name, position_id, year_id)
SELECT name, new_positions.id, $inserted_year_id AS last_year_id
FROM employees
JOIN positions AS old_positions ON old_positions.id = employees.position_id
                                AND old_positions.year_id = employees.year_id
JOIN positions AS new_positions ON new_positions.name = old_positions.name
                                AND new_positions.year_id = $inserted_year_id
WHERE employees.year_id = 1
于 2012-10-06T04:34:51.563 回答
2

我认为您应该阅读有关数据库规范化的信息。复制数据会导致维护问题和错误报告。

如果您使用如下所示的不同设计,则在员工更改职位、终止或终止职位之前,将没有任何内容可插入。还有很多其他方法可以解决这个问题,但您应该尽量减少冗余(即每个员工只有一个副本),然后单独跟踪随时间变化的数据。在你尝试实现这样的东西之前,还要阅读外键。

positions:
-- If you are keeping track of the years that each position is active, 
-- using dates provides simplicity.  Note: this design assumes that positions 
-- are never reactivated after being deactivated.
------------------------------------------------
| id | name        | DateActive | DateInactive |
------------------------------------------------
|  1 | Director    | 01/01/2011 |              |
|  2 | Manager     | 01/01/2011 |              |
------------------------------------------------

employees:
---------------------------------------------------------------
| id | name                     | DateHired  | DateTerminated |
---------------------------------------------------------------
|  1 | Employee A               | 01/01/2011 |                |
|  2 | Employee B               | 01/01/2011 |                |
|  3 | Employee C               | 01/01/2011 | 10/01/2012     |
---------------------------------------------------------------

EmployeePositionRelationships 
--If you are keeping track of time that each employee held a position
-- Employee A has been a Director since 1/1/2011
-- Employee B was a Manager from 1/1/2011 to 10/6/2012. Then they became a Director
-- Employee B was a Manager from 1/1/2011 to 10/1/2012. Then they were terminated
--------------------------------------------------------
EmployeeId | PositionId | DateStarted | DateEnded      |
--------------------------------------------------------
1          |  1          | 01/01/2011 |                |
2          |  2          | 01/01/2011 | 10/6/2012      |
3          |  2          | 01/01/2011 | 10/1/2012      |
2          |  1          | 10/6/2012  |                |
--------------------------------------------------------
于 2012-10-06T06:11:43.083 回答