我有很多带有“狗”标签的文章。我现在有一个名为“猫”的新标签。我想将标签“猫”添加到所有已经有标签“狗”的文章中。一些文章有这两个标签。我不想用“猫”重新标记这些。
换句话说,如果一篇文章有标签“狗”但还没有标签“猫”,我想给它添加标签“猫”。这是我写的脚本:
<?php
# Get "cat" tag id
$sql = "SELECT `id`
FROM tags
WHERE name = 'cat'
LIMIT 1";
$cat_tag = mysql_fetch_assoc(mysql_query($sql));
# Get "dog" tag id
$sql = "SELECT `id`
FROM tags
WHERE name = 'dog'
LIMIT 1";
$dog_tag = mysql_fetch_assoc(mysql_query($sql));
######################################
# Get all nodes tagged with "dog"
$sql = "SELECT `node_id`
FROM node_tags
WHERE `tag_id` = '" . $dog['id'] . "'";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)) {
# Check to see if current node has "cat" tag already
$sql = "SELECT COUNT(*)
FROM node_tags
WHERE `node_id` = '" . $row['node_id'] . "'
AND `tag_id` = '" . $cat['id'] . "'";
$check_already_exists = mysql_fetch_assoc(mysql_query($sql));
# If node doesn't already have "cat" tag, then add it
if($check_already_exists == '0') {
$sql = "INSERT INTO `node_tags`(node_id, tag_id)
VALUES('" . $row['node_id'] . "', '" . $cat['id'] . "')";
mysql_query($sql);
}
}
?>
我希望能够直接从我的 MySQL 管理器工具中运行它。所以它不能有任何 PHP,但应该是一个大型 SQL 查询。这个怎么做?