views:

81

answers:

7
INSERT INTO 
product_search_table (id, score) 
VALUES (
SELECT product_id, 50 FROM cart_product WHERE product_id = 555
)

Is giving me an error. What am I doing wrong?

555 is going to be replaced with a variable.

+5  A: 

Try

INSERT INTO 
product_search_table (id, score) 
SELECT product_id, 50 FROM cart_product WHERE product_id = 555
Simon Nickerson
+1  A: 

Brackets around the select look suspect - as does the VALUES. Try:

INSERT INTO product_search_table (id, score) 
SELECT product_id, 50 FROM cart_product WHERE product_id = 555
Will A
+1  A: 

Use:

INSERT INTO product_search_table 
(id, score) 
SELECT product_id, 50 
  FROM cart_product 
 WHERE product_id = 555

When using a SELECT statement in an INSERT statement, you don't use the VALUES keyword or enclose the SELECT in brackets.

OMG Ponies
+7  A: 

You're using incorrect syntax for an INSERT...SELECT query.

Here's how you'd do it properly:

INSERT INTO product_search_table (id, score) 
SELECT product_id, 50 FROM cart_product WHERE product_id = 555

More information: MySQL 5.1 Reference: INSERT..SELECT Syntax

Faisal
Nice to see that we're all on the same page. :)
Faisal
+4  A: 

Seems to be a syntax issue. VALUES doesn't belong in this type of query.

http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

George Marian
And, here I was surprised that no one had answered this question yet...
George Marian
A: 

My guess is that the subquery could return multiple rows (in theory). So how could multiple rows fit into a single field? You could try to make sure the queried data is a scalar value.

(I'm also not too sure about the second field/the 50 value).

edit: VALUES is used in insert statements people. See this page

edit2: You're only selecting data for the first column, where is the data for the second column?

Laurens Ruijtenberg
VALUES is sometimes used for INSERT, but not in this case.
Simon Nickerson
+1  A: 

you can't do that with the VALUES.

for example:

INSERT INTO 
 product_search_table (id, score) 
SELECT product_id,50 FROM cart_product WHERE product_id = 555
northpole