Getting SQL error for replace function - mysql

I'm not to sure why this is happening and i've been trying to figure it out now for a while.
I've got the follow code
SELECT (MAX(replace(replace(replace(`sku`,'PA1-',''),'TES-',''),'BOX-',''))+1) AS maxValue FROM `product` WHERE `sku` LIKE '%PA1-TES-BOX%'
This was working a while back and nothing has changed code wise, I can only assume that a server changes has caused this to return 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 'maxValue FROM ``product`` WHERE ``sku`` LIKE '%PA1-TES-BOX%'
Basically this SQL was built to find the first 3 section of this SKU code and return the ending + 1 so 002 would then return 003 to ensure unique sku codes.
Maybe the replace function has changed, i'm not entirely sure.
Does anyone have any ideas why this suddenly is throwing the error above?

I don't see an obvious syntax error. But assuming the number is the last hyphenated item in the sku, the code could more easily be written as:
select (substring_index(sku, '-', -1) + 1) as maxvalue
. . .
One possibility for the syntax error is that an unprintable character crept in around the as.

Related

MySQL syntax error(the query works perfectly fine in mysql but somehow ends up giving error in jsp)

I am trying to update database in mysql but when i use the query in mysql it works perfectly fine......but when i update it through jsp it gives me an error as
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
this is query of JSP: (in content i do not add any closing brackets like ')')
insert into location_"+username+"
values('"+coordinates+"','"+content+"',"+latitude+","+longitude+");
i use stmt.executeUpdate(q1);
the above query is stored in q1..
this is query in mysql:(this works absolutely fine)
insert into location_user1 values('{lat: 19.2222, lng: 73.9781}','random coordinate from database', 19.2222, 73.9781);
i have found out that this error is somewhat common..yet i could not find out any solution...PLease Help!! Thank You!! This error is so frustrating..
Try printing q1 and execute the printed output in MySQL.
At jsp end, the query is not getting properly built and this only is leading to the issue.

SQL ERROR - Error sql syntax

I am getting an error that I dont know how to deal with.
I am running the same code without issue for another column but for this column it refuses to work.
SELECT * FROM Players WHERE Character = 'momo' // This one wont work
SELECT * FROM Players WHERE Class = 'Fighter' // this one works
Character is a VARCHAR and Class is TEXT. I have tried changing Character to TEXT and I still get the same issue. The value 'momo' exists in the table.
ERROR: Couldn't connect to server. 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 '= ''' at line 1
Edit:
I am editing this incase someone find this and wants to know how it was fixed. User by the name of ueerdo Pointed out that I should use quotations and when I did, it worked. So I started looking into why it happened and I found out the SQL reserves Character for something else so it is something that I can't use unless it is in quotations.
It is best to delimit identifiers to prevent possible collision with reserved words and keywords.
SELECT * FROM `Players` WHERE `Character` = 'momo'

SQL trimming syntax error

I've just finished 'SAM's Teach Yourself SQL in 24 Hours' book, and I've been given the task of listing which of our active site's "site_code" match their respective "site_path"s with the prefix '/var/www/html' removed. I've been stuck for the past hour or so trying to figure out what I'm doing wrong and don't know what else to try at this point.
This is what I've got so far:
SELECT site_name FROM example_tbl
WHERE active = 1
AND site_code IN
(SELECT TRIM(LEADING '/var/www/html/' FROM site_path) FROM example_tbl;);
Trying to run that's giving me this:
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 '' at line 4
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 ')' at line 1
I'm using Mysql ver 14.14 Dustrib 5.5.38 on Ubuntu ver 14.10
The nested query works fine by itself, and if I get rid of everything from 'AND' on, the first part works fine as well, so I'm not sure why they won't work together :\
Any help would be much appreciated!
EDIT:
Sorry, I should have been more specific!
'site_code', 'site_path' and 'site_name' are all columns in the table 'example_tbl', and I'm trying to get a list site names to print out like this
Input: /var/www/html/example.company.com
Output: example.example.com
EDIT2:
Oh and for that example above:
The 'site_name' would be: example.company.com
The 'site_code' would be: example
And the 'site_path' would be: /var/www/html/example.company.com
Remove the extra semicolon.
SELECT site_name FROM example_tbl
WHERE active = 1
AND site_code IN
(SELECT TRIM(LEADING '/var/www/html/' FROM site_path) FROM example_tbl);
The following query uses the MySQL SUBSTRING function instead of TRIM:
SELECT site_name
FROM example_tbl
WHERE active = 1 AND
SUBSTRING(site_path, 1, 13) = '/var/www/html' AND
SUBSTRING(site_path, 14) = site_code
I don't think you needed a subquery to solve your problem. Also, I included a condition in the WHERE clause to also check that the site_path begins with 'var/www/html'. If you expect all your sites to begin with this, then feel free to remove this condition.

Syntax error 256 in WHERE clause

I am trying to update a field in my table and I keep getting this syntax error.
global $conn, $strTableName;
db_exec("UPDATE equipment SET EContractNum = " . $_SESSION[$strTableName."_masterkey1"] . " WHERE EContractNum = " . $values['EContractNum'], $conn);
Here is the error:
Error type: 256 Error Description: 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 econtractnum=35867111' at line
1
I have looked at several searchs that are similair to mine but I cannot figure out what I am doing wrong. I am fairly new at this so it is probably something simple. I just cant seem to make it work. Thanks for any help.
$_SESSION[$strTableName."_masterkey1"] is probably empty, or a string that needs to be quoted.
Also, don't put the raw values of variables into queries like that. Use a framework or prepared statements. It's good for security and it would also prevent this kind of error (well, it'd turn it into a different kind of error, at least).

Mysql syntax error in scrapy

Hi i am working on scrapy and writing a pipeline and in that i had a query which should write the data in to mysql database
tx.execute("""INSERT INTO example_table (book_name,price)
VALUES (%s,%s)""",
(item['book_name'],
item['price'],)
)
I am getting the following errors two errors below
(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")
(1241, 'Operand should contain 1 column(s)')
I dont know whats wrong in this query but i am unable to save the data in to database.
Can any one have a idea of this.
You forgot to add % while executing
x.execute("""INSERT INTO example_table (book_name,price)
VALUES (%s,%s)""",%
(item['book_name'],
item['price'])
)
You have added an additional comma at last remove it. following is the correct statement. Kindly try.
x.execute("""INSERT INTO example_table (book_name,price)
VALUES (%s,%s,%s,%s,%s,%s)""",
(item['book_name'],
item['price'])
)