我正在尝试将项目从一个数据库的表名称项目复制到另一个数据库的名为items的表中。我下面有一个mysql脚本,但它会出错。
INSERT INTO walmart.items
(title, location, created_at, updated_at, image_url, url, price)
VALUES
(
SELECT title, location, created_at, updated_at, image_url, url, price
FROM warehouse.items
WHERE url LIKE '%walmart.com%'
);
我很确定这主要是语法问题,而且我的逻辑是正确的。我尝试了一些不同的配置,但仍然遇到类似的错误。
收到错误:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT title, location, created_at, updated_at, image_url, url, price FROM old_t' at line 1
在战争中:
desc items;
输出
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| title | varchar(255) | YES | | NULL | |
| location | varchar(255) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| image_url | varchar(255) | YES | | NULL | |
| url | varchar(255) | YES | UNI | NULL | |
| price | int(11) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+------------+--------------+------+-----+---------+----------------+
在沃尔玛
desc items;
输出:
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| title | varchar(255) | YES | | NULL | |
| location | varchar(255) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| image_url | varchar(255) | YES | | NULL | |
| url | varchar(255) | YES | UNI | NULL | |
| price | int(11) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+------------+--------------+------+-----+---------+----------------+
答案 0 :(得分:4)
您插入选择错误:
INSERT INTO walmart.items (title, location, created_at, updated_at, image_url, url, price)
SELECT
title,
location,
created_at,
updated_at,
image_url, url,
price
FROM warehouse.items
where url LIKE '%walmart.com%';
以下是参考:http://dev.mysql.com/doc/refman/5.7/en/insert-select.html
答案 1 :(得分:1)
不是MySQL专家,但我认为应该是:
INSERT INTO walmart.items (title, location, created_at, updated_at, image_url, url, price) SELECT title, location, created_at, updated_at, image_url, url, price FROM warehouse.items where url LIKE '%walmart.com%';
基本上你有什么,但没有VALUES关键字。这就是SQL Server中的内容,我认为它不会与MySQL不同。
当您手动输入值而不是从表中选择时,将使用VALUES关键字。
答案 2 :(得分:1)
只需删除VALUES
即可正常使用