0

I am trying to run this SQL Query:

INSERT INTO controldata (field,value) 
VALUES (customer_billing_product_type, 
  SELECT name from customer_billing_product_types)

to insert all rows in the customer_billing_product_types table into the controldata table with the field always being customer_billing_product_type but i get an SQL Error saying:

#1064 - 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 name from customer_billing_product_types)' at line 1 
1
  • Writing each SQL instruction (e.g. INSERT INTO, SELECT, etc.) below one another helps greatly in solving syntax errors like these. Commented Apr 30, 2014 at 7:47

2 Answers 2

4

The INSERT as you wrote it expects to insert one record, but then in values an entire set of records is returned. Try to change your query to

INSERT INTO controldata(field, value)
SELECT customer_billing_product_type, t.name FROM customer_billing_product_types t;

This selects all t.name values from customer_billing_product_types, adds customer_billing_product_type as column and inserts all the results into controldata.

Sign up to request clarification or add additional context in comments.

Comments

0

You are using wrong syntax try below:

INSERT into controldata (field1) SELECT name from customer_billing_product_types;

If you want to keep a field as constant then you can use as per below:

INSERT into controldata (field,value) SELECT 'customer_billing_product_types',name from customer_billing_product_types;

1 Comment

You forgot the constant field.

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.