2016-09-26 95 views
0

我有一个数据库,其中包含通过djangocms占位符字段添加的产品文本内容的多余数据库表。我期待将这些内容移回到单个表格中。MySQL删除链接表并将3个表合并为一个

我的三个表是:

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表的混乱。

+0

你为什么要这么做? AFAIK,你会这样做_de_ - 规范化你的数据库。 –

+0

额外的字段最初被添加为一个Django的cms占位符字段,一直困扰着我们的网站管理员。他们希望能够在一个地方编辑整个产品,所以我觉得将这些信息转移回产品本身是一个好主意。 –

+0

如果因为其他原因需要链接表,那么我认为没有理由删除'cmsplugin_text'表。如果你不需要链接表,那么你应该完全删除它。 –

回答

0

当您在products_product更新添加列:

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 
+0

花了一点时间格式化,但这就是我以前的做法,简单而完美。谢谢! –