简体   繁体   English

如何在两个不同的表中添加相同的值

[英]how can I add same value in two different tables

im trying to add custom list of tags using excel file and I have done most of the work. 我试图使用excel文件添加自定义标签列表,我已完成大部分工作。 but i am confused that how can I add same value in 2 different tables columns. 但我很困惑,如何在2个不同的表列中添加相同的值。

i had inserted the values in 我插入了值

INSERT INTO `steve`.`wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, 'tag99', 'tag99', '0');

and

INSERT INTO `steve`.`wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, '21', 'post_tag', '', '0', '0');

now i want to add term_id in both table .. could somebody tell me how can i do that? 现在我想在两个表中添加term_id ..有人可以告诉我该怎么办?

AND term_id is database generated value. AND term_id是数据库生成的值。

INSERT INTO `steve`.`wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, LAST_INSERT_ID, 'post_tag', '', '0', '0');

If I follow correctly your first query inserts into a table and creates a new id for that record, then you want to use that ID in the next query to insert into a different table right? 如果我正确地按照你的第一个查询插入表并为该记录创建一个新的id,那么你想在下一个查询中使用该ID来插入另一个表吗? If that's the case the above query should work for your second one using LAST_INSERT_ID for the term_id value. 如果是这种情况,上述查询应该适用于您的第二个查询,使用LAST_INSERT_ID作为term_id值。

To update the question with your new code it should, I emphasize should, work with this: 要用新代码更新问题,我应该强调,应该使用这个:

 $sql2 = "INSERT INTO " . $table2 . " (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, LAST_INSERT_ID, 'post_tag', '', '0', '0')";

        $wpdb->query($sql);
        $wpdb->query($sql2);

The way you have it above you are overwriting your first query before you execute it. 你在上面的方式是在执行它之前覆盖你的第一个查询。

Also I don't understand how you're inserting nulls into an auto-increment field, that alone should be throwing an error. 此外,我不明白你如何将空值插入自动增量字段,单独应该抛出一个错误。 Honestly both your queries should be leaving their base ids (the auto increment ones) out of the query entirely like this: 老实说,你的查询应该完全按照以下方式从查询中删除它们的基本ID(自动增量)

$sql2 = "INSERT INTO " . $ sql2 =“INSERT INTO”。 $table2 . $ table2。 " ( term_id , taxonomy , description , parent , count ) VALUES (LAST_INSERT_ID, 'post_tag', '', '0', '0')"; “( term_idtaxonomydescriptionparentcount )VALUES(LAST_INSERT_ID,'post_tag','','0','0')”;

Use mysql_insert_id() 使用mysql_insert_id()

mysql_query("INSERT INTO `steve`.`wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, 'tag99', 'tag99', '0')");

$lastInsertId = mysql_insert_id();

mysql_query("INSERT INTO `steve`.`wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, '" . $lastInsertId . "', 'post_tag', '', '0', '0')");

NOTE: To use mysql_insert_id(), term_id in wp_terms must be set to auto increment. 注意:要使用mysql_insert_id(),必须将wp_terms term_id设置为自动增量。

你需要使用ALTER表并将该列的定义/约束添加到另一个表中,这是手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM