簡體   English   中英

具有多種數據類型的閉包表?

[英]Closure table with multiple data types?

我最近一直在刷我的MySQL,我需要創建一個包含分層數據的數據庫。

我有幾種不同類型的數據需要以樹格式表示,但不知道如何去做。

例如,假設我有一個人,可以雇用或受雇於其他人。 這些人中的每一個都可能有設備檢查,每件設備必須有名稱,描述和更換部件清單,每個更換部件必須有成本等。

我看到關閉表的大多數例子都集中在它們處理論壇或線程注釋的方式上。 如何制作具有多種數據類型的閉包表?

這是一個快速而骯臟的例子:

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 |

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM