【发布时间】:2013-10-25 13:48:10
【问题描述】:
我在表 exp_weblog_titles t 中有一系列博客条目。在表 exp_categories c 中按类别分类,并由表 exp_category_posts cp 连接。
类别中有父子关系。此时,我想将我目前归类为 4810 类别子项的所有条目,并将这些条目本身归类为 4810 类别。
这是我为此操作提出的(未经测试的)查询:
INSERT INTO exp_category_posts (entry_id,cat_id)
(select distinct(t.entry_id), '4810'
from
exp_weblog_titles t
left join
exp_category_posts cp ON t.entry_id = cp.entry_id
left join
exp_categories c ON c.cat_id = cp.cat_id
where
t.weblog_id = 5 and c.parent_id = 4810)
但我想避免在可能存在相同记录的 category_posts cp 中插入新的连接记录。
我无法理解“不存在的地方”。它应该是插入操作、子选择的一部分吗?怎么都想不通。
应该是这样的:
IF NOT EXISTS
SELECT * FROM exp_category_posts
where cat_id = 4810 and entry_id = $thecurrentrow
但是我要引用当前行吗?
这是在 ExpressionEngine 1 中。
更新来自 Mats 的回答: (entry_id, cat_id) 上没有唯一键。不确定添加一个的后果。
【问题讨论】:
-
只是一个注释。如果没有键,则每次插入都需要进行全表扫描,这在性能方面非常昂贵。如果您要执行大量此类插入操作而不是在应用程序层中执行工作,那么在表上建立索引可能会很好。 更新:表格中有多少行?