簡體   English   中英

將一個SQL表中的行鏈接到不同表中的多行,

[英]Link row in one SQL table to multiple rows in different table,

我正在制作一個基於Web的工具來解析URL。 它適用於兩個表。 一個用於URL,另一個用於找到的單詞及其解析/出現次數。 這是一個簡化的示例:

URLs

url-id      url
----------------------------------------------------
1           www.example.net/this-is-a-sentence
2           www.example.org/this-is-another-sentence


Words

word-id  word         category        occurences
----------------------------------------------------
1        this         pronoun         2
2        is           verb            2
3        a            det             1
4        sentence     noun            2
5        another      det             1

我試圖找出一種方法來將URL表中的每個URL鏈接到Words表中的每個單詞。 這樣,如果用戶決定刪除其中一個URL,則所有適當的出現次數都可以減少。 我對SQL完全陌生,所以不知道它如何在單元格中存儲信息,但是我想對URL中的所有單詞ID進行動態調整大小的列表。

url-id      url                                         words
---------------------------------------------------------------------------
1           www.example.net/this-is-a-sentence          1,2,3,4
2           www.example.org/this-is-another-sentence    1,2,3,5

我樂於接受關於在SQL中組織數據的完全不同方式的建議。

注意-刪除時,我無法簡單地再次解析URL,因為在可能的情況下,需要用戶輸入來驗證解析。

您需要這樣的橋接表:

url-id      word-id     (represents, not part of table)
-------------------
1           1           url-id 1 has word-id 1 (this)
1           2           url-id 1 has word-id 2 (is)       
1           3           url-id 1 has word-id 3 (a)
1           4           url-id 1 has word-id 4 (sentence)
2           1           url-id 2 has word-id 1 (this)
2           2           url-id 2 has word-id 2 (is)
2           5           url-id 2 has word-id 5 (another)
2           4           url-id 2 has word-id 4 (sentence)

這稱為多對多關系。 一個URL可以有許多單詞,而一個Word可以屬於許多URL。 是一篇很好的文章,描述了SQL中的不同關系。

暫無
暫無

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

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