0

I am trying to run an MySQL query to copy over data from an old table (ps__product_review/rate) to a new table (ps_product_comment/grade) based on review ID (id_product_comment). But I am a bit lost on the SQL query, this is what I have but keep getting errors.

  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];

Can anyone help write the correct query?

Edit:Essentially I am trying to populate the Grade column in the new table below.

Old table (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      |       |
 +--------------------+----------+-------+
1
  • 1
    When you ask a question here, you should provice as much information as possible. For example what errors you get. Any way you should remove square brackets. In docs square brackets mean that this part of query is optional. Commented Aug 22, 2014 at 11:48

2 Answers 2

1

If you want to update table with data from another table, use UPDATE with JOIN

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;
Sign up to request clarification or add additional context in comments.

Comments

1

Remove the square brackets and I think you are missing the JOIN(since you are using that in your where clause):

   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;

4 Comments

This didn't work, it copied the Rank/Grade from the old table and pasted it at the bottom of the new table, not into the correct column/row. I have updated question to clarify what I meant.
@Naz:- You need to add the column name in the insert and select clause and you are done!
Just tried that but it's still adding the data to end of the table rather than filling in the blank column.
If you want to fill columns, you have to use UPDATE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.