I am trying to run the below query (in Sequel Pro), but I am getting the following syntax 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 'IF
(EXISTS(SELECT username FROM USER WHERE username = "TestObserver"))
{SELECT *' at line 1'
Code:
IF EXISTS (SELECT username FROM USER WHERE username = "TestObserver")
SELECT * FROM USER WHERE username = "TestObserver"
You can't use an if block outside of a function. Instead just run the subsequent SQL statement:
SELECT * FROM `USER` WHERE username = "TestObserver"
If there is no username with testobserver value it will already return nothing all by itself.
Related
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
I am trying some sammple query in mysql client but the transaction syntax does not work]
MySQL database-2:3306 ssl TestDb SQL > START TRANSACTION
-> select * from mvcc where glyph = 'a'
-> COMMIT
-> ;
And it reports syntax error :
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 'select * from mvcc where glyph = 'a'
COMMIT' at line 2
This seems to be a very simple query and it's too easy to understand why it fails. I can run the select statement without any issue. Any idea?
You need a semicolon at the end of each of the queries.
START TRANSACTION;
SELECT * FROM mvcc WHERE glyph = 'a';
COMMIT;
I am getting the error
Could not update users table: 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 'Long = '-2.8867589' WHERE Username = 'test1'' at line 1"
when i run this sql statement:
if($updateuser=mysql_query("UPDATE Users SET Long = '$long' WHERE Username = '$_SESSION[myusername]'")){
echo 'Users table updated';
}
LONG is a reserved word in MySQL (see here). It is a bad name for a column, but if you use it, then you need backticks:
UPDATE Users
SET `Long` = '$long'
WHERE Username = '$_SESSION[myusername]';
You should also switch to mysqli and use parameterized queries.
How to use a stored function instead of an explicitly written table name.
My query:
select "col_name" from get_table("col_name") where id = 2;
get_table(col_name) is my stored function which returns the table name after having processed column name.
I get error message
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 '("col_name") where id = 2' at line 1
I also tried to do like this:
DECLARE tableName varchar(30);
set tableName = get_table("col_name");
...
select "col_name" from tableName where id = 2;
This time I get error:
no such a table - tableName
So im creating a procedure that accepts an argument namee and returns all users whose name is a substring of namee
create procedure search_res(IN namee varchar(50))
begin
select * from usr where name like %namee%;
end
But im getting the following 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 '%namee%; end' at line 3.
What is the correct syntax?
Use CONCAT function
select * from usr where name like CONCAT('%',namee,'%');