INSERT INTO with WHERE? - mysql

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,...);

Related

Can't run Insert and Select LAST_INSERT_ID() in the same query?

I am using node to connect to mysql and I need to run an insert and then immediately run a select last_insert_id().
insert into data_temp values (null, '{"test":{"id":12,"otherdata":"x","otherdata2":"y"}}');
SELECT LAST_INSERT_ID();
In mysql workbench this query works but gives me an error saying:
SELECT LAST_INSERT_ID() )
Error Code: 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 ')' at line 2 0.047 sec
It actually returns the correct id but because it is throwing an error on the last part node is catching the error.
There is an issue in your insert query you just missed to put the name of your columns
Do like this
insert into data_temp (`col1`, `col2`) values (null, '{"test":
{"id":12,"otherdata":"x","otherdata2":"y"}}'); SELECT LAST_INSERT_ID();
In place of col1and col2 put your columns name then it will work for you.

mySQl insert into an exsisting table Syntax Error

I am trying to add these value in the "employee" table, but it shows me an error that I can't fix, any help?!
The Syntax:
INSERT INTO `employee`( 'UserName',`ID`, `firstname`, `middlename`,'lastname','Address','department','salary','password','phonenumber','email','LoginStatus')
VALUES ('medo',1','ahdjh','rgetrhytyiu','rgthyu','','2','100000','','24741585285','da#hash.com','1')
The error:
Error: INSERT INTO employee( 'UserName',ID, firstname,
middlename,'lastname','Address','department','salary','password','phonenumber','email','LoginStatus')
VALUES
('medo',1','ahdjh','rgetrhytyiu','rgthyu','','2','100000','','24741585285','da#hash.com','1')
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 ''UserName',ID, firstname,
middlename,'lastname','Address','department','sa' at line 1
Try this:
Remove quotes from column--
INSERT INTO employee( UserName,ID, firstname, middlename,lastname,Address,department,salary,password,phonenumber,email,LoginStatus) VALUES ('medo',1','ahdjh','rgetrhytyiu','rgthyu','','2','100000','','24741585285','da#hash.com','1')
Just remove the (username,ID,......)
Directly write
Insert Into Tablename Values(.....);
And in the Values section you have a ` this in the ID field.Remove that as well.

Error with MySQL insert query

insert into sws(grade) values(100) where student_id='$session_id' & cys='$get_id2'
It shows error as
You have an error in your sql syntax; check manual that corresponds to your MYSQL server version for right syntax to use near 'WHERE student_id='21' & cys='java' at line 1
Use Update Command not Insert
Replace the & with AND (read about MySQL AND syntax).
INSERT syntax cannot have WHERE clause as you used, you might want to use UPDATE.
Basic UPDATE syntax :
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

INSERT values from one table to another using SQL Query

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;

Mysql insert command syntax

I am using MYSQL 5.1, I am trying to write an insert statement in my java class. It sounds very simple but i tried and getting exception. I am sure there is a syntax error with my code. below is the snippet could any one help me out here.
ID is in primary key
atm_ID is varchar
trasn_id is varchar
sysDate is Date
rest is int
Please help me out.
Error:
SQL statement is not executed!com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '£10, £20, £50, £100, trans_Id) VALUES (7,'hello','hello','2','2','2','2','2','he' at line 1`
Code:
String query = "INSERT INTO atm_data (ID,atm_Id, sysDate, availBalance, £10, £20, £50, £100, trans_Id) VALUES (7,'hello','2010-09-15 01:20:06','2','2','2','2','2','hello')";
It looks like you simply need to escape the column names that start with £ with backticks:
INSERT INTO atm_data (ID, ... `£10`, `£20`, `£50`, `£100` ...
Test case:
CREATE TABLE tb (`£10` int);
Query OK, 0 rows affected (0.05 sec)
INSERT INTO tb (£10) VALUES (10);
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 '?10) VALUES (10)' at line 1
INSERT INTO tb (`£10`) VALUES (10);
Query OK, 1 row affected (0.00 sec)