3

最近、MySQL をブラッシュアップしており、階層データを含むデータベースを作成する必要があります。

ツリー形式で表現する必要があるいくつかの異なるタイプのデータがありますが、それを行う方法がわかりません。

たとえば、私には他の人を雇うことができる、または雇われる人がいるとしましょう。これらの人々はそれぞれ機器をチェックアウトすることができ、各機器には名前、説明、および交換部品のリストが必要であり、各交換部品にはコストなどが必要です。

私が目にするクロージャ テーブルのほとんどの例は、フォーラムやスレッド化されたコメントを処理するのにどれだけ優れているかに焦点を当てています。複数のデータ型を持つクロージャ テーブルを作成するにはどうすればよいですか?

4

1 に答える 1

1

これは簡単で汚い例です:

select * from person

| pID | name      | employedBy |
+-----+-----------+------------+
|   1 | John Doe  |          2 |
|   2 | Joe Smith |       NULL |
|   3 | Meg Ryan  |          3 |

select * from equipment

| eqID | eqName   | eqDescription     | eqOwner | eqCheckedOutTo |
+------+----------+-------------------+---------+----------------+
|    1 | stuff    | just some stuff   |       3 |           NULL |
|    2 | table    | a table           |       1 |           NULL |
|    3 | computer | PC computer       |       3 |              2 |
|    4 | 3table   | table with 3 legs |       2 |           NULL |

select * from parts;

| partID | partName     | partCost |
+--------+--------------+----------+
|      1 | desktop1     |   499.99 |
|      2 | monitor13x13 |   109.95 |
|      3 | windows95    |    10.00 |
|      4 | speakers     |    30.00 |
|      5 | tabletop     |   189.99 |
|      6 | table leg    |    59.99 |

select * from equipmentParts

| epID | eqID | partID | quantity |
+------+------+--------+----------+
|    1 |    3 |      1 |        1 |
|    2 |    3 |      2 |        2 |
|    3 |    3 |      3 |        1 |
|    4 |    2 |      5 |        1 |
|    5 |    2 |      6 |        4 |
|    6 |    4 |      5 |        1 |
|    7 |    4 |      6 |        3 |

次のようにクエリできます。

select name,eqName,e.eqID,partName,partCost,quantity,(quantity*partCost) AS totCost
from person p
inner join equipment e ON e.eqOwner=p.pID
inner join equipmentParts ep ON ep.eqID=e.eqID
inner join parts pa ON ep.partID=pa.partID

| name      | eqName   | eqID | partName     | partCost | quantity | totCost |
+-----------+----------+------+--------------+----------+----------+---------+
| John Doe  | table    |    2 | tabletop     |   189.99 |        1 |  189.99 |
| John Doe  | table    |    2 | table leg    |    59.99 |        4 |  239.96 |
| Meg Ryan  | computer |    3 | desktop1     |   499.99 |        1 |  499.99 |
| Meg Ryan  | computer |    3 | monitor13x13 |   109.95 |        2 |  219.90 |
| Meg Ryan  | computer |    3 | windows95    |    10.00 |        1 |   10.00 |
| Joe Smith | 3table   |    4 | tabletop     |   189.99 |        1 |  189.99 |
| Joe Smith | 3table   |    4 | table leg    |    59.99 |        3 |  179.97 |

または各機器のコストを要約します。

select name,eqName,sum(quantity*partCost) AS totCost
from person p
inner join equipment e ON e.eqOwner=p.pID
inner join equipmentParts ep ON ep.eqID=e.eqID
inner join parts pa ON ep.partID=pa.partID
group by e.eqID

| name      | eqName   | totCost |
+-----------+----------+---------+
| John Doe  | table    |  429.95 |
| Meg Ryan  | computer |  729.89 |
| Joe Smith | 3table   |  369.96 |
于 2013-12-30T19:16:37.120 に答える