【问题标题】:Perform a INSERT .. SELECT in MySQL with derived tables在 MySQL 中使用派生表执行 INSERT .. SELECT
【发布时间】:2017-03-07 08:49:11
【问题描述】:

我正在尝试将连接查询的结果插入到另一个表中。

INSERT INTO temp(
SELECT b.id, b.number, b.attempt FROM(
SELECT number FROM duplicate_numbers)a 
JOIN calls b ON b.number=a.number));

不带 INSERT INTO 子句的连接查询本身可以正常工作并返回数据集。但是上面的查询给出了 SQL 语法错误

【问题讨论】:

  • 错误信息是什么?
  • 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 'SELECT b.id 附近使用的正确语法
  • 右括号太多了。

标签: mysql select sql-insert


【解决方案1】:

像这样更改查询语法:

CREATE TABLE temp (`id` int, `number` int, `attempt` int);

INSERT INTO temp (`id`, `number`, `attempt`)
SELECT b.id, b.number, b.attempt FROM (
  SELECT number FROM duplicate_numbers
) a 
JOIN calls b ON b.number=a.number

工作演示:http://sqlfiddle.com/#!9/24f9f

【讨论】:

  • @Prashan 同样的错误? 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 b.id ?
  • 您的 SQL 语法有误;检查与您的 MySQL 服务器版本相对应的手册,以在第 4 行的 ')' 附近使用正确的语法
  • @Prashan temp 表是空的吗?否则需要运行UPDATE 语句
  • 临时表为空
  • @Prashan MySql 上的版本是什么?
猜你喜欢
  • 2014-08-08
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 2011-01-22
  • 1970-01-01
  • 2012-02-21
相关资源
最近更新 更多