0
我想知道將一個表中的一行與另一個表中的多行關聯的最佳做法。跨表格關聯多行的最佳方法
比方說,我有以下兩個表:
table_users
id | username
------------------------------------------------------------------------
1 | user1
2 | user2
3 | user3
table_texts
id | text
------------------------------------------------------------------------
1 | This is a secret text, that only user2 and user3 should see.
現在我唯一的解決方案是創建一個第三個表:
table_user_text_relation
id | text_id | user_id
------------------------------------------------------------------------
1 | 1 | 2
2 | 1 | 3
,然後選擇這樣的:
SELECT
table_texts.text
FROM
table_users, table_texts, table_user_text_relation
WHERE
table_users.id = table_user_text_relation.user_id
AND
table_texts.id = table_user_text_relation.text_id
這很好......但是,如果我有6000個用戶可以訪問500個文本,table_user_text_relation會將th必須有3.000.000行建立多對多的關係?
有沒有更好/更聰明的方法來做到這一點?
MySQL將輕鬆處理3百萬行。只需在users_to_texts表中建立索引即可。這裏沒有什麼可以改進的地方。 – user4035