1

年、従業員、役職の 3 つのテーブルがあります。これらのテーブルにこれらのデータが既にあるとします。

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:
----------------
| 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      |
---------------------------------------------------------

表の 3 行目と 4 行目のクエスチョン マークに注意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

データベースの正規化について読むべきだと思います。データをコピーすると、メンテナンスの問題や誤ったレポートが発生します。

次のような別の設計を行った場合、従業員がポジションを変更するか、終了するか、ポジションが廃止されるまで、挿入するものは何もありません。これにアプローチする方法は他にもたくさんありますが、冗長性を最小限に抑え (つまり、各従業員のコピーを 1 つだけにする)、時間の経過と共に変化するデータを個別に追跡する必要があります。このようなものを実装しようとする前に、外部キーについても読んでください。

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 に答える