Syntacts error in mysql query - mysql

Someone gave me this query to delete community from a database on my server using phpMyAdmin, it worked when he use it so I asked him to send it to me, but I get a error
MySQL said:
Documentation
#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 * FROM `connections` WHERE 1" :
delete from connections where communit' at line 1
I did a search on the error but could not figure it out.
"SELECT * FROM `connections` WHERE 1" :
delete from connections where community="XYZ"

You should separate your queries with ; and not :

The error you are receiving as you've shown it is because this line is incorrect:
SELECT * FROM `connections` WHERE 1"
First because it's ending in an unnecessary double quotation mark. MySQL does not use double quotes but single quotes, and even still the other single quote is not there to match it.
The single quotes are also a problem for community="XYZ", this should read: community = 'XYZ'
Secondly, you don't have a condition for your where statement, you must be missing something like:
WHERE columnName = 1;
If you were trying to select everything from connections, you can just remove that where clause all together.
EDIT
In addition, MySQL queries are separated by a semi-colon, not a colon, so MySQL will not realize you are trying two different queries.

Related

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'

MySQL error "An alias was previously found" when an alias isn't in the query

I have a very odd error while trying to perform an update on a database. This is on an Ubuntu 16.04 server using MySQL 5.7.19-0ubuntu0.16.04.1. The query is:
UPDATE athlet_teamseason SET offkeyreturners = 'test' WHERE athlet_teamseason.id = 29701;
The MySQL error is:
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 'offkeyreturners = 'test' WHERE athlet_teamseason.id = 29701' at line 1
I am doing this in phpMyAdmin, and it gives a bit more information:
2 errors were found during analysis.
An alias was previously found. (near " " at position 50)
An alias was previously found. (near "'test'" at position 51)
If I try this update directly in the phpMyAdmin user interface (search for record, edit field value, submit form) it works, and the query shown is:
UPDATE athlet_teamseason SET offkeyreturners = 'test' WHERE athlet_teamseason.id = 29701;
which appears to be identical. HOWEVER, if I do a string comparison between the two I get:
So while they appear to be the same, there is a difference somewhere.
The queries were created from a table in a database, using concatenation and referencing cells in a source table. For example:
="UPDATE athlet_teamseason SET offkeyreturners = '"&data!I2&"' WHERE athlet_teamseason.id = "&data!A2&";"
I have thousands of these and they all produce the same error. I've done this dozens of times in older servers, might be an issue with MySQL 5.7?
Thanks to Uueerdo, I eliminated non-printing characters in my query.

MYSQL: Check if table exists does not work

IF OBJECT_ID(N`db_291702_2`.`aaCoRrankingDateManage`, N'U') IS NOT NULL
BEGIN
PRINT 'Table Exists'
END
What is wrong with this? Why do I get errors?
Neither of the suggested ways in how-to-check-if-a-table-exists-in-sql-server/ works for me.
PS. "does not work" means errors like
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 ''db_291702_2'.'aaCoRrankingDateManage' LIMIT 0, 30' at line 1
Additional info:
I am using phpMyAdmin, my two databases are called db_291702_1 and db_291702_2, the latter has two tables, one of them is called aaCoRrankingDateManage
If you want to escape table or column names then use backticks
select `group` from table1
A static string must be included in quotes
select * from users where name = 'john'
And the syntax of every DB engine is a little bit different. The above works for MySQL, but SQL-Server has a different syntax. There you use brackets [] to escape names.
But you only need to escape names if you use reserved words. You don't have to escape everything.
The given source code is no MySQL Code.

MySQL Syntax with DELETE FROM?

I have this bit of SQL that always returns an error, though I can't find why it is returning the error. I have connected to the database with no errors. I'm running PHP 5.2.17, MySQL 5.5.25a, and Apache 2.4.2.
The SQL:
DELETE FROM mail WHERE to=1
The 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 'to=1' at line 1
TO is a reserved word, you need to use backticks:
DELETE FROM mail WHERE `to` = 1
By adding backticks on the column name escapes in from MySQL Reserved Word
DELETE FROM mail WHERE `to`=1
if the column to is not e.g. INT or DEC you should make it to = "1"

MySQL query sanity check, for update query

I have this query
update user_remember_me set
when='2012-07-06 05:44:27',
hash='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7'
where user = '21';
and I am getting this error
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 'when='2012-07-06 05:44:27', hash='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7' wher' at line 1
Im failing to miss the connection here, I've used simple updates like this everywhere without issue til this, maybe Im getting to tired, but this is gonna drive me nuts til I have an answer
When is a key word in mysql, change the column name
or you can use it as
`when`='2012-07-06 05:44:27'
when is a reserved word in mysql
update user_remember_me set
`when`='2012-07-06 05:44:27',
`hash`='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7'
where user = '21';
So you must backtick your column
when is a keyword within MySQL. You have to escape it, if you want to use it as a column identifier (as you should with all column identifiers!):
UPDATE user_remember_me
SET
`when`='2012-07-06 05:44:27',
`hash`='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7'
WHERE `user` = '21';