mysql - 如何从另一个表插入表

标签 mysql

我有两个表,art 和 art_tag_art。 这是栏艺术表:

id
title
slug
image

这张值(value)艺术表 enter image description here

这是栏 art_tag_art

id
art_id
tag_id

值 art_tag_art enter image description here

我想将所有art.id复制到art_tag_art.art_id,同时将所有art_tag_art.tag_id(已经存在)复制到新行中。

我尝试并搜索了几种mysql语法插入并选择:

select id into @aid from art;
select tag_id into @tid from art_tag_art;
insert into art_tag_art (art_id, tag_id) values (@aid,@tid);


INSERT INTO art_tag_art (art_id, tag_id) 
SELECT art.id, art_tag_art.tag_id 
FROM art, art_tag_art order by id;

但没有任何效果。这次我收到一条错误消息:错误代码:1364。字段“id”没有默认值

预期值: enter image description here

最佳答案

试试这个:

select id=@aid from art order by id; select tag_id=@tid from art_tag_art; insert into art_tag_art (art_id, tag_id) values (@aid,@tid);<br/> INSERT INTO art_tag_art (art_id, tag_id) SELECT art.id, art_tag_art.tag_id FROM art, art_tag_art order by id;

关于mysql - 如何从另一个表插入表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41677497/

相关文章:

mysql - 将每个 ID 的外键数量与另一个数据库进行比较

如果只有今天可用,MySQL BETWEEN 子句不会返回任何内容。为什么?

mysql - 如何创建 View ,以便在基表更改时它们也会更改?

php - 按小时对数据库数据进行分组

python - 如何使用 NumPy 正确从 SQL 数据库读取字符串

mysql - 搜索 1 亿条记录的性能

java - 如何选择 MySQL 表中的最后一行?我没有任何自动递增键

mysql - 在 Go 中从 MySQL 迁移到 Mongodb 时处理 NULL

mysql - 统计 child 数量的好方法?

php - 使用 AngularJS 和 PHP 向 MySQL 表插入数据后显示空值