MySQL + JAVA - 无法将表插入另一个表

时间:2014-08-05 11:08:12

标签: java mysql sql

我似乎无法将旧表插入新表中。非常奇怪

代码:

INSERT INTO 'newhawk_playewadwds' (`player_id`,'player') SELECT `player_id`,'player' FROM 'hawk_playewadwds';

错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You
have an error in your SQL syntax; check the manual that corresponds to your MySQ
L server version for the right syntax to use near ''newhawk_playewadwds' (`playe
r_id`,'player') SELECT `player_id`,'player' FROM 'h' at line 1

额外信息:

两个表都存在并且具有稍微不同的列结构(一个具有varchar(50),而另一个没有)。这就是我在声明中包含直接列的原因。

3 个答案:

答案 0 :(得分:5)

引号是字符串的分隔符。使用反引号来转义列名和表名,而不是引号。

INSERT INTO `newhawk_playewadwds` (`player_id`,`player`) 
SELECT `player_id`,`player` 
FROM `hawk_playewadwds`;

但实际上你只需要逃避reserved words

答案 1 :(得分:1)

尝试将'字符替换为表名,将列player替换为

INSERT INTO `newhawk_playewadwds` (`player_id`,`player`) 
SELECT `player_id`,`player` 
FROM `hawk_playewadwds`;

答案 2 :(得分:0)

在查询中使用反引号。您正在使用反引号和单引号。使用反引号或删除单引号:

-- This will work (Backticks only)
INSERT INTO `newhawk_playewadwds` (`player_id`,`player`) 
SELECT `player_id`,`player` 
FROM `hawk_playewadwds`;

-- This will also work (No backticks and no single quotes)
INSERT INTO newhawk_playewadwds (player_id,player) 
SELECT player_id,player 
FROM hawk_playewadwds;

另一件事,正如你提到的列结构不同,

  1. 如果newhawk_playewadwds有列> 50,而hawk_playewadwds已经< 50,这应该不是问题
  2. 如果hawk_playewadwds' has > 50 and newhawk_playewadwds`已经<50,请执行以下任一操作:

    1. 将新表newhawk_playewadwds的大小增加到hawk_playewadwds
    2. 的相同大小
    3. 在select as中使用Substring:

      INSERT INTO `newhawk_playewadwds` (`player_id`,`player`) 
          SELECT `player_id`,SUBSTRING(`player` ,1,50)
          FROM `hawk_playewadwds`;