简体   繁体   English

3个mysql表之间的关系

[英]Relations between 3 mysql tables

I have 3 mysql tables: events, artists and (artist) descriptions. 我有3个mysql表:事件,艺术家和(艺术家)描述。 All of them are in many-to-many relations. 它们都是多对多关系。

Table 'events': 表“事件”:

 ID | eventTitle | etc.
-----------------------
  1 | event 1    |
  2 | event 2    |
etc.

Table 'artists': 表“艺术家”:

 ID | artistName | etc.
-----------------------
  1 | artist 1   |
  2 | artist 2   |
etc.

Table 'descriptions': 表“说明”:

 ID | artistDesc             | etc.
----------------------------------
  1 | artist 1 description 1 |
  2 | artist 1 description 2 |
  3 | artist 2 description 1 |
  4 | artist 2 description 2 |
  5 | artist 3 description 1 |
etc.

I made also junction tables events_artists and artists_desctriptions . 我还制作了联结表events_artistsartists_desctriptions Both of them have only 2 foreign keys and serve only for linking event, artist and description IDs. 它们都只有两个外键,仅用于链接事件,艺术家和描述ID。

Notice in my descriptions table - each artist can have many descriptions. 请注意我的描述表中-每个艺术家可以有许多描述。 That actually means that each description belongs to only one specific event. 实际上,这意味着每个描述仅属于一个特定事件。 =) =)

If I do a query like this: 如果我这样查询:

$q = "SELECT 
        events.*,artists.*,descriptions.*,events_artists.*,artists_descriptions.* 
        FROM 
        events,artists,descriptions,events_artists,artists_descriptions 
        WHERE 
        events.eventID = events_artists.eventID AND 
        events_artists.artistID = artists.artistID AND 
        artists.artistID = artists_descriptions.artistID AND 
        artists_descriptions.descID = descriptions.descID"; 

I will get all the descriptions for a particular artist. 我将获得特定艺术家的所有描述。 But none of descriptions will be aware which event they belong to... What I want to display to user is something like this: 但是没有一个描述会知道它们属于哪个事件...我想向用户显示的是这样的:

EVENT 1 活动1
artist 1 (artist 1 description 1) 艺术家1(艺术家1描述1)
artist 2 (artist 2 description 2) 艺术家2(艺术家2描述2)

EVENT 2 活动2
artist 3 (artist 3 description 6) 艺术家3(艺术家3描述6)
artist 1 (artist 1 description 3) 艺术家1(艺术家1描述3)

etc. 等等

Should I make a junction table for event-description relation? 我应该为事件描述关系创建连接表吗? If I do, I don't know exactly how to use it, uff! 如果可以,我不知道该怎么使用,哎呀! =) Or maybe my problem isn't solvable with a simple query? =)也许我的问题无法通过简单的查询解决? Should I do something with php too? 我也应该用php做些什么吗? Sorry but I am totally confused =) 抱歉,但我完全感到困惑=)

I hope I managed to explain my problem properly... 我希望我能正确解释我的问题...

Thanks in advance for any help! 在此先感谢您的帮助!

您应该将events_artistsdescriptions表组合events_artists ,只有1个表可以将Artist与事件和说明链接。

Can you modify the tables you already have? 您可以修改已有的表格吗? If so, and if each artist description can have only one artist and one event, then I would modify your schema to be: 如果是这样,并且如果每个演出者描述只能有一个演出者和一个事件,那么我将您的架构修改为:
Table event as is. 表事件保持原样。
Table artists as is. 餐桌艺术家照原样。
Table descriptions: 表说明:

ID | artistDesc                     | artistId | eventId | etc.
------------------------------------------------------------------
 1 | description of artist at event | 4        | 1       | ...

Then you can select all descriptions for an event with: 然后,您可以通过以下方式选择事件的所有描述:

$query = "SELECT 
    events.*,artists.*,descriptions.*
    FROM 
    events,artists,descriptions
    WHERE 
    artists.artistID = descriptions.artistID AND 
    events.eventID = descriptions.eventID AND
    events.eventID = $eventIdYouWant";

You can change the last row of that query to either events.event_name = $eventNameYouWant or artists.artist_name = $artistNameYouWant and it will work the same as if you were directly specifying the id. 您可以将该查询的最后一行更改为events.event_name = $eventNameYouWantevents.event_name = $eventNameYouWant artists.artist_name = $artistNameYouWant ,它的工作原理与直接指定id相同。

您已经说过3个表之间的关系..所以我认为答案非常明显..您需要在一个表中有1个表artist_id,event_id和description_id,而不是像现在那样将其分为两个表。

I think you should alter your table structure, if you can. 我认为,如果可以的话,您应该更改表结构。 It will result in a neater design. 这将导致更整洁的设计。 Make a table, artist_event_description which contains all the IDs as foreign keys, instead of the two junction tables, it will help you to find out, to which event and artist a description belongs. 创建一个表,artist_event_description,其中包含所有ID作为外键,而不是两个联结表,它将帮助您找出描述属于哪个事件和艺术家。

Another thing you can do is to include two more columns in the Description table, eventId and artistId (these will be foreign keys) and remove the junction tables. 您可以做的另一件事是在Description表中再添加两列eventId和artistId(它们将是外键)并删除联结表。 This way you will directly get all the information you need by just doing a SELECT over the description table. 这样,您只需对描述表进行SELECT即可直接获得所需的所有信息。

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

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