【发布时间】:2015-01-12 22:54:17
【问题描述】:
我有一个名为 'users' 的表。在 'users' 表中,包含一个名为 'chapter_id' 的字段。 另一个名为“chapters”的表包含特定主题的所有章节名称,与它们的 id 相关。我想在用户的“章节”中添加多个“id”到“chapter_id”。 我可以在“章节”中的“用户”中添加一个“章节 ID”。 这是我的用户表:
-------------------------
id | name | chapter_id |
-------------------------
1 | ABC | 8 |
-------------------------
这是我的“chaprers”表:
-------------------------
id | name | subject |
-------------------------
1 | XYZ | PQR |
-------------------------
我用来从 'chapters' where 'id' = 'some id' in table 'users' 中选择名称的查询是:
SELECT `name`, subject FROM chapters WHERE id = (SELECT chapter_id FROM users where id= 11)
为了插入chapter_id,我写道:
UPDATE `users` SET `chapter_id`= (SELECT id FROM chapters WHERE id = 5) WHERE id =11
现在我想在 'chapter_id' 中添加多个 'id' !这样做的查询是什么?
【问题讨论】:
-
您正在寻找多对多关联。
-
您能否添加更多示例数据和预期输出。
-
嗯。
chapter_id=select id from chapters where id=5?那有什么意义呢?为什么不chapter_id=5? -
您不能在一个值中插入多个
chapter_id。 -
不要把自己和我们搞混了,INSERT是用来在表中插入一行的。 UPDATE 用于更新一个(或多个)已经存在的行!