SQL INSERT INTO查询语法

时间:2014-08-22 11:45:55

标签: mysql

我正在尝试运行MySQL查询,以根据审核ID(id_product_comment)将旧表(ps__product_review / rate)中的数据复制到新表(ps_product_comment / grade)。但是我在SQL查询上有点迷失,这就是我所拥有但却不断出错。

  INSERT INTO ps_product_comment [(grade)] 
   SELECT rate
   FROM ps__product_review
   [WHERE ps__product_review.id_product_comment=ps_product_comment.id_product_comment];

有人可以帮忙写出正确的查询吗?

编辑:基本上我正在尝试填充下表新表中的成绩列。

旧表(ps__product_review)

+--------------------+----------+-----+
| id_product_comment | Comment  | Rate|
+--------------------+----------+-----+
|  1                 | Good     |  2  |
|  2                 | Great    |  5  |
|  3                 | OK       |  3  |
|  4                 | Brill    |  4  |
|  5                 | OK       |  3  |
|  6                 | Average  |  2  |
|  7                 | Bad      |  1  |
+--------------------+----------+-----+


New Table (ps_product_comment)
 +--------------------+----------+-------+
 | id_product_comment | Comment  | Grade |
 +--------------------+----------+-------+
 |  1                 | Good     |       |
 |  2                 | Great    |       |
 |  3                 | OK       |       |
 |  4                 | Brill    |       |
 |  5                 | OK       |       |
 |  6                 | Average  |       |
 |  7                 | Bad      |       |
 +--------------------+----------+-------+

2 个答案:

答案 0 :(得分:1)

删除方括号,我认为你错过了JOIN(,因为你在where子句中使用了):

   INSERT INTO ps_product_comment (grade)
   SELECT rate
   FROM ps__product_review inner join ps_product_comment on 
   ps__product_review.id_product_comment=ps_product_comment.id_product_comment;

答案 1 :(得分:1)

如果要使用其他表中的数据更新表,请使用带有JOIN的UPDATE

UPDATE ps_product_comment 
JOIN ps__product_review
ON ps__product_review.id_product_comment = ps_product_comment.id_product_comment
SET ps_product_comment.grade = ps__product_review.rate;