MySQL删除链接表并将3个表连接成一个

时间:2016-09-26 14:15:48

标签: mysql join django-cms

我有一个数据库,它有一个无关的产品文本内容数据库表,它是通过djangocms占位符字段添加的。我希望将这些内容移回一个表格。

我的三张桌子是:

products_product:

+------------+------------+
| product_id | content_id |
+------------+------------+
|        374 |       1919 |
+------------+------------+

cms_cmsplugin:

+----------------+------+
| placeholder_id |  id  |
+----------------+------+
|           1919 | 2042 |
+----------------+------+

和cmsplugin_text:

+------------------+------------------+
| cmsplugin_ptr_id |       body       |
+------------------+------------------+
|             2042 | <p>some_html</p> |
+------------------+------------------+

我希望删除链接表并将cmsplugin_text主体直接添加到products_product表中,其字段名称为text_content。到目前为止,我一直在查看this个问题,并提出以下建议:

INSERT INTO
products_product(content_id, text_content)

SELECT
p.content_id,
cpt.body as text_content

FROM products_product p
JOIN cms_cmsplugin cms ON p.content_id = cms.placeholder_id
LEFT JOIN cmsplugin_text cpt ON cms.id = cpt.cmsplugin_ptr_id

遗憾的是,这种情况并没有奏效,只是弄乱了我的products_product表。

1 个答案:

答案 0 :(得分:0)

在products_product update:

中添加列后
UPDATE products, cms_cmsplugin, cmsplugin_text 
SET products.body = cmsplugin_text.body
WHERE product.content_id = cmsplugin_text.placeholder_id
AND cmsplugin_text.id = cmsplugin_ptr_id