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
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.
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;
Related
I am very new to Sql, so bear with me. I have run a query the following query:
INSERT INTO adhoc_dt.`table` (id, name) VALUES(53098974, 'John');
however, by accident I run it twice. I would like to remove the duplicate. How can that be done?
I tried
INSERT INTO adhoc_dt.`table` (id, name) VALUES(53098974, 'John');
but get an error:
SQL ERROR [1064][42000]: 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
If your table name include special char of reserved word then you should enclose the table name with backticks:
`table`
But looking to your question, the table name seems:
`adhoc_dt.`table`
In this case, the correct syntax for delete is:
DELETE FROM adhoc_dt.`table`
WHERE id = 53098974 AND name = 'JOHN'
but in this way you delete all the rows with:
id = 53098974 AND name = 'JOHN'
You have to follow steps
truncate table
apply unique constraint on ID
run insert script
I'm facing a problem with select inside insert statements.
I've take a look at the questions similar to this but still the query is not working.
first, I'm working with MySQL version 5.6.24 with engine InnoDB, and I'm trying to insert this row:
INSERT INTO form (SELECT course_name FROM course WHERE course_id ='1' ), '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad')
I want the first column to be retrieved (which is only one value), but not the other.
I've tried many syntax formats, with VALUES, with semicolon, with more parentheses, etc... but non work. Here is the error:
#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 ' '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','no' at line 1
Thanks.
Use an INSERT-SELECT:
INSERT INTO form
SELECT course_name, '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad'
FROM course
WHERE course_id = '1'
I'm running this SQL query:
$regSql = "INSERT INTO orderregel(AANTAL) VALUES('$aant') WHERE TAAK_ID='$TkID'";
I also tried this one:
$regSql = "INSERT INTO orderregel(AANTAL) VALUES('$aant') WHERE TAAK_ID=$TkID";
But it both gave this error:
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 'WHERE TAAK_ID='13'' at line 1
What might be the solution?
insert into syntax doesn't have where
INSERT INTO table_name
VALUES (value1,value2,value3,...);
probably you wanted to use insert into select
INSERT INTO table
( column name(s))
SELECT column name(s) from table where condition
OR
IF ( condition )
INSERT INTO table_name
VALUES (value1,value2,value3,...);
I'm attempting to inset values into every row where one of the column values contains a specific string.
INSERT INTO `cases_index`(`related`) VALUES (`Alabama`) WHERE `court` LIKE '%Alabama%'
So int eh above example I have a row with three columns:
court | federal | related
I want to insert the word 'Alabama' into the 'related' column for any row where the court column contains the word 'Alabama' (i.e. 'District Court of Alabama').
The above line spits out the following syntax error:
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 'WHERE cases_index(court) LIKE '%Alabama%'' at line 1
Any ideas what I am doing wrong?
Apart from the syntax error in your query, if you want to update existing rows you should use an UPDATE query:
UPDATE cases_index
SET related='Alabama'
WHERE court LIKE '%Alabama%'
Just for completeness, INSERT queries add new rows to the table, so a WHERE clause in an INSERT is not allowed.
While the UPDATE answers are what you are looking for, INSERT does allow WHERE clause.
Refer to MySQL documentation
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
But the answer to this is what trgdor wrote:
UPDATE cases_index
SET related='Alabama'
WHERE court LIKE '%Alabama%'
Why does the following show a mysql 1064 error?
I have a the following table:
daily_note (id, time (timestamp), rate_id, note )
On this table I'm executing the following insert:
INSERT INTO daily_note (note)
VALUES ('this is a note')
WHERE rate_id = 37
AND time > '2011-05-22 00:00:00'
Cannot perform insert:
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 'WHERE rate_id = 37 AND time > '2011-05-22 00:' at line 3'
You can't INSERT using a WHERE statement. If you want to modify existing records based on some condition/criteria, use an UPDATE statement instead of INSERT.
INSERT with WHERE makes no sense.
INSERT creates new rows, whereas WHERE specifies criteria for retrieving/updating existing rows.
Read up about the syntax and meaning of the statement that you're trying to use, before trying to use it.
Code should read:
UPDATE DAILY_NOTE
SET field='this is a not'
WHERE rate_id = 37
AND time > '2011-05-22 00:00:00'