简体   繁体   English

mysql查找表加入其他3个表,有没有更好的方法?

[英]mysql lookup table to join 3 other tables, is there a better way?

I have three tables 我有三张桌子

USER
USER_ID
USERNAME
[additional fields]

ASSET
ASSET_ID
DISPLAY_NAME
[additional fields]

PAGE
PAGE_ID
[additional fields]

The USER table is what you would expect and standard USER table. USER表是您所期望的,并且是标准USER表。 Each user can add assets to their library, so there is a 1 to many relationship between USER and ASSET . 每个用户都可以将资产添加到他们的库中,因此USERASSET之间存在一对多的关系。 Additionally, each ASSET is made up of multiple pages, and each PAGE can have multiple notes that is just for that user to see. 此外,每个ASSET由多个页面组成,每个PAGE可以具有多个注释,仅供该用户查看。 My initial thought is to have a lookup table between all three tables: 我最初的想法是在所有三个表之间都有一个查询表:

USER_ASSET_PAGE
UAP_ID
USER_ID
ASSET_ID
PAGE_ID
NOTE

So now I have to query the USER_ASSET_PAGE table to get notes that correspond to a specific page of a specific document for a distinct user: 因此,现在我必须查询USER_ASSET_PAGE表以获取与特定用户的特定文档的特定页面相对应的注释:

SELECT NOTE 
FROM USER_ASSET_PAGE
USER_ID = {user_id} AND ASSET_ID = {asset_id} AND PAGE_ID = {page_id}

Is there a better way to structure this type of data? 有没有更好的方法来构造这种类型的数据? It feels like there maybe complications down the road. 感觉可能会遇到麻烦。

Create 4 tables 创建4张桌子

**USER**
USER_ID
USERNAME
[additional fields]

**ASSET**
ASSET_ID
USER_CHILD_ID
DISPLAY_NAME
[additional fields]

**PAGE**
PAGE_ID
ASSET_CHILD_ID
[additional fields]

**NOTE**
NOTE_ID
PAGE_CHILD_ID
NOTE_TEXT
[additional fields]

then to get data, write select statement: 然后获取数据,编写select语句:

select NOTE_TEXT from NOTE inner join PAGE on PAGE_CHILD_ID = PAGE_ID inner join ASSET on ASSET_CHILD_ID = ASSET_ID where USER_CHILD_ID = :USERID 

Well based on your description, I personally would use the following scenario: 根据您的描述,我个人将使用以下情形:

USER {UserID,User_Name},...;
ASSET {AssetID,DISPLAY_NAME,UserID (FK)};
PAGE {PageID,AssetID (FK)...};
NOTE {NoteID,Note,PageID (FK)};

then, to query for notes from a specific user,asset and page: 然后,要查询来自特定用户,资产和页面的注释:

Select n.NOTE
From NOTE n
inner join PAGE p on p.ID=n.PageID
inner join ASSET a on a.AssetID=p.AssetID
inner join USER u on u.UserID=a.UserID
where u.UserId=x and p.PageID=y and a.AssetID=z

This is obviously a normalized attempt. 这显然是正常的尝试。 Of course it all depends on what your goals are and the expected data to be stored. 当然,这完全取决于您的目标是什么以及要存储的预期数据。 With low amount of data, your denormalize design would be enough. 使用少量数据,您的非规范化设计就足够了。 However with major amounts of data, hundreds of users with several assets with hundreds of pages, you could face a great growth on your USER_ASSET_PAGE table. 但是,在拥有大量数据的情况下,数百名用户拥有数以百计的资产的数百页,您的USER_ASSET_PAGE表可能会面临巨大的增长。

My approach would serve better an operational purpose, rather than a "data exploration" one. 我的方法将比“数据探索”更好地用于操作目的。 Later on if you would need to perform some major aggregation on these tables it would be a good idea to denormalize data like you did and look for some optimization as well. 稍后,如果您需要对这些表执行一些主要的汇总,那么像您一样对数据进行非规范化并寻找一些优化也是一个好主意。

Just model the data like you describe it. 只需按照描述的方式对数据进行建模即可。

Additionally, each ASSET is made up of multiple pages 此外,每个ASSET由多个页面组成

So add an ASSET_ID column to table PAGE . 因此,将ASSET_ID列添加到表PAGE I am assuming that a PAGE is not shared between several ASSETs. 我假设在多个ASSET之间不共享PAGE。

and each PAGE can have multiple notes that is just for that user to see. 并且每个PAGE可以有多个注释,仅供该用户查看。

So create a new table NOTE with NOTE_ID , PAGE_ID , USER_ID and NOTE . 因此,创建一个新表NOTENOTE_IDPAGE_IDUSER_IDNOTE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM