gpt4 book ai didi

mysql - 如何插入选择...哪里不存在选择?

转载 作者:行者123 更新时间:2023-11-29 13:30:04 24 4
gpt4 key购买 nike

我在表 exp_weblog_titles 中有一系列博客条目。它们按表 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) 上没有唯一的键。不确定添加一个的后果。

最佳答案

假设 (entry_id, cat_id)exp_category_posts 中的唯一键,您应该能够使用 INSERT IGNOREINSERT IGNORE 将跳过插入时会生成重复键错误的记录。

您可以在引用手册 http://dev.mysql.com/doc/refman/5.5/en/insert.html 中找到更多信息.

关于mysql - 如何插入选择...哪里不存在选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19591640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com