简体   繁体   English

我将如何插入影响多个表的值?

[英]How would I go about inserting values that affect multiple tables?

My schema is this:我的架构是这样的:

Tables:
Titles ->         ID |     TitleID    | TitleName | ArtistNameFull | Length
Artists->         ID | ArtistNameRoot
ArtistRelation->  ID | TitleID | ArtistID | Relationship

ArtistNameFull is artist name including: "feat, with, including, featuring, presents, etc." ArtistNameFull 是艺术家姓名,包括:“feat、with、包括、特色、礼物等。”

ArtistNameRoot is just the artist without "feat, with including, featuring, etc." ArtistNameRoot 只是没有“壮举,包括,特色等”的艺术家。

The relationship between them in ArtistRelation is in another table which has indexes 1-15 and their corresponding "suffix" essentially.他们在 ArtistRelation 中的关系在另一个表中,该表本质上具有索引 1-15 及其相应的“后缀”。 As in, index 3 would be "featuring" and index 7 would be "with".如在,索引 3 将是“特色”,而索引 7 将是“与”。

I have about 1000 rows of data that I need to insert into my tables, which is just values of (Title, Artist, length).我有大约 1000 行数据需要插入到我的表中,这只是(标题、艺术家、长度)的值。

Methods so far: My methods so far have been to place all this data that I need inserted into a temporary table itself and then call sql functions that select unique values between the Title table and the temporary table and then insert them, which works if I'm inserting into just the Titles table, but I also need to insert into Artists which is a little more tricky because I need to get rid of anything but the root artist.到目前为止的方法:到目前为止,我的方法是将我需要插入的所有这些数据放入临时表本身,然后调用 sql 函数来选择 Title 表和临时表之间的唯一值,然后插入它们,如果我我只是插入到 Titles 表中,但我还需要插入到 Artists 中,这有点棘手,因为我需要摆脱除根艺术家之外的任何东西。 So then I tried using php to grab data from my temporary table and using regular expressions to get just the root artist, which works wonders for getting just the root artist, but connecting all this together and figuring out how to insert both into artist relation when there could already be artists from previous insertions is hard to wrap my head around.然后我尝试使用 php 从我的临时表中获取数据并使用正则表达式来获取根艺术家,这对于仅获取根艺术家产生了奇迹,但是将所有这些连接在一起并弄清楚如何将两者插入到艺术家关系中可能已经有来自之前插入的艺术家,我很难想象。

Anyway, I guess I just need words of wisdom for if I'm going about this in a really inefficient way, or this is how it is usually done and I just have to keep going.无论如何,我想我只需要智慧之言,因为如果我以一种非常低效的方式进行此操作,或者这通常是这样做的,我只需要继续前进。

PHP is much better than SQL at manipulating strings. PHP 在处理字符串方面比 SQL 好得多。 So keep doing that.所以继续这样做。 Also using the temp table is a valid method.使用临时表也是一种有效的方法。

You will need to use PHP to pull all of the data from the temp table.您将需要使用 PHP 从临时表中提取所有数据。

For each result parse out the artist name and insert it into artists.对于每个结果,解析出艺术家姓名并将其插入到艺术家中。

After each insert you want to get the last ID that was inserted (PDO and mysqli_ have methods for this) and then store that ID and the artist name in an array so that the ID can be used later.每次插入后,您都希望获取最后插入的 ID(PDO 和 mysqli_ 具有用于此的方法),然后将该 ID 和艺术家姓名存储在一个数组中,以便以后可以使用该 ID。

Then insert the title information.然后插入标题信息。

After each insert get the last ID that was inserted and use it and the artist ID to insert a record into the relations table.每次插入后获取最后插入的 ID,并使用它和艺术家 ID 将记录插入到关系表中。

Does this help?这有帮助吗?

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

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