Mysql Select Last inserted row - mysql

I'm creating a stored procedure using phpmyadmin for mysql 5.5.55 .
INSERT INTO drama_ (value1,value2,value3,value4,value5,value6)
VALUES (val1,val2,0,0,0,0);
SELECT LAST_INSERT_ID();
but i'm getting a syntax error in SELECT LAST_INSERT_ID();
EDIT :
You have an error your SQL syntax: check the manual that corresponds to your MYSQL server version for the right syntax to use near 'SELECT LAST_INSERT_ID()' at line 4

Related

Check if record exists in table before inserting new data

SQL Noob here.
Im using PHP PDO with named placeholders to insert data to my DB but I want to check if the data exists before I insert it.
Here is my statement:
IF NOT EXISTS (SELECT * FROM dc_line_items_transactions WHERE order_id = '3784732' AND product_id = '435062') INSERT INTO dc_line_items_transactions (order_id,refund_id,user_id,transaction_date,product_id,line_item_id,line_subtotal,line_vat,line_total,dc_timestamp) VALUES (:order_id,:refund_id,:user_id,:transaction_date,:product_id,:line_item_id,:line_subtotal,:line_vat,:line_total,:dc_timestamp)
My PDO code works fine as Its being used by other classes to insert varying different types of data.
My error message is as follows:
SQLSTATE[42000]: Syntax error or access violation: 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 'IF NOT EXISTS (SELECT * FROM dc_line_items_transactions WHERE order_id= '3784732' at line 1

#1064 - SQL syntax ERROR, how to solve it?

I have 3 tables[table_a, table_b, dataz] in a my database, when I create a TRIGGER as follow,
CREATE TRIGGER trigz AFTER INSERT ON table_a
FOR EACH ROW
IF EXISTS (SELECT * FROM table_b WHERE table_b.uid = '123') THEN
INSERT INTO dataz
VALUES('123');
Getting ERROR as,
#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 5
I don't understand what I am missing here...
I am using WAMP Server 2.4, anybody have solution ?
EDIT 1(for Nigel Ren)::
SQL :
CREATE TRIGGER trigz AFTER INSERT ON table_a FOR EACH ROW
IF EXISTS (SELECT * FROM table_b WHERE table_b.uid = 1) THEN
BEGIN
INSERT INTO dataz(userid) VALUES('123');
END;
END IF;
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 '' at line 4

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

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)

Error inserting data into mysql

I am trying to insert data from a csv file into mysql using BigDump.
It stops on line 2, with the error:
"Query: INSERT INTO location VALUES
(1,"O1","","","",0.0000,0.0000,, );
MySQL: 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 1"
If I run the statement from withing phpmyadmin, it says:
"#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 1"
What can I do to make the data get into the database?
Thank you.
The commas with no values looks sketchy to me.
INSERT INTO location VALUES (1,"O1","","","",0.0000,0.0000,, );
should probably be
INSERT INTO location VALUES (1,"O1","","","",0.0000,0.0000,NULL,
NULL);
Does your insert statement contain values for every column in the table? If not, you have to name the columns.
For example,
insert into location (col1, col2, col3) values (1, 2, 3);
If you show us the structure of the LOCATION table you can get better answers.