Error near LIMIT but only when I use colons - mysql

I'm trying to run this query:
SELECT wins FROM players WHERE auth = '[U:1:123456789]' LIMIT 1;
But I get the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 0, 25' at line 1
However, when I remove LIMIT 1 it works.
If I change the query to look like this, it also works:
SELECT wins FROM players WHERE auth = '[123456789]' LIMIT 1;
I'm very confused, what am I doing wrong? It seems like colons just break the query.
Edit: CREATE TABLE
CREATE TABLE `players` (
`auth` varchar(32) NOT NULL,
`name` varchar(32) NOT NULL DEFAULT '< blank >',
`wins` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`auth`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Three Four options (starting with the most likely):
0) Your version of your SQL Database or your version of PHPMyAdmin is very, very out of date and should be updated ASAP.
1) You're running the SQL on a PHP PDO interface which uses Colons to mark an input variable. See here.
2) MariaDb uses colons as special characters and they need to be properly escaped.
3) Somehow you have two LIMITs (as mentoned by Tadman, that PhpMyAdmin or your own PHP interface is adding a LIMIT) when one is not needed.

It ended up being an issue with phpMyAdmin's (the client I used to run the queryh) SQL parser.
As of 4.7.1 of phpMyAdmin, it should be fixed.
https://github.com/phpmyadmin/sql-parser/commit/4f85b6d8d3a3ddcf6ff216c116cf305978e9a3d2

Related

How to create random number for a column in mysql with DEFAULT Constraint?

The DEFAULT constraint has no problem in accepting string or current date values. What i need is an constraint that will create an random 4 digit number every time an entity is created. I tried the following code but it returns an syntax error.
ALTER TABLE client_number ADD(code INT(4) DEFAULT FLOOR(RAND()*9999)+1111);
The above statement returns 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 'floor(rand()*9999)+1111)' at line 1
Need solution.
The documentation is quite clear:
The DEFAULT value clause in a data type specification indicates a
default value for a column. With one exception, the default value must
be a constant; it cannot be a function or an expression.
The expression you have is not a constant, so it doesn't work. You would need to use a trigger instead.
Run the below query
ALTER TABLE table_name ADD field_name INT(4) NOT NULL DEFAULT (rand() * (9999 - 1000) + 1000) AFTER some_feild;
The result gives a random number between 1000-9999

MySQL query with enum values

I've the following structure in MySQL 5.6.19.
CREATE TABLE `metadata` (
`md5` char(32) NOT NULL,
`match` enum('none','md5','similarity') DEFAULT NULL
)
And I got an error doing a query like this:
select * from metadata where match = 'md5';
The error is:
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 '= 'md5'' at line 1
There're multiple entries in the table and rows that could match the query. But MySQL refuse to do it. Any idea about the reason?
Thanks!
MATCH is reserved keyword in MySQL: http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-5.html. You should enclose your field name in backticks to make it work:
select * from metadata where `match` = 'md5';

Error in creatinng a field in the table

I have problem in creation a field name staus which type-BOOLEAN. and length is 1.
When I press the go button then this Massage Arrived
SQL query:
ALTER TABLE `abcd` ADD `status` BOOLEAN( 1 ) BINARY NOT NULL DEFAULT NULL
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 '(1) BINARY NOT NULL DEFAULT NULL' at line 1
I can't solve the problem. I don't know where the error occurring.
Please Help me to solve the problem.
Thank you.
The correct syntax for ALTER TABLE requires COLUMN after ADD
ALTER TABLE `abcd` ADD COLUMN ...
ALTER TABLE `abcd`
ADD COLUMN `status` BIT NOT NULL DEFAULT 0
If you want to create only a single bit field use BIT. You had 2 data types in your statement.
Your default value was null but you don't want to allow that: NOT NULL. Use 0 as default value instead.
For MySQL 5.0.3 and higher, you can use BIT. The manual says:
As of MySQL 5.0.3, the BIT data type is used to store bit-field values. A type of BIT(M) enables storage of M-bit values. M can range from 1 to 64.
Otherwise, according to the MySQL manual you can use bool and boolean which are at the moment aliases of tinyint(1):
Bool, Boolean: These types are synonyms for TINYINT(1). A value of zero is considered false. Non-zero values are considered true.

query not work for single quote apostrophe

phpmyadmin query not work for single quote / apostrophe.
Not Work
ALTER TABLE 'about_team' CHANGE 'position' 'pp' INT( 11 ) NOT NULL
Work:
ALTER TABLE `about_team` CHANGE `position` `pp` INT( 11 ) NOT NULL
Same query but not work, gives 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 ''about_team' CHANGE 'position' 'pp' INT(11) NOT NULL' at line 1
it is because when you using single quote, it just means it is a STRING. Whereas BACTICK (the second query) means escaping a column.
'about_team' is not equal with `about_team`
'about_team' is STRING
`about_team` is a Table Name
Actually backticks enclosing the names are optional since the names used where not on MySQL Reserved Keyword List.
MySQL Reserved Keywords
Usually, single quotes are used around values while backticks are for table names and column names.

Ruby Mysql - load .sql file and execute

I'm trying to load a .sql file in a ruby script into a string and then execute it.
For some reason i'm getting a Syntax error, however if i copy and paste the statements directly into mysql it works just fine.
Here's how i do it:
text = File.read(src_sql_file)
new_text = text.DOINGSOMEGSUBSTUFF
#dbh.select_db(dbname)
sql = #dbh.prepare(new_text)
sql.execute()
i've also tried this:
sql = #dbh.prepare(new_text) do |sth|
sth.execute()
end
and i always get:
Error message: 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
'CREATE TABLE `the_logs` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PR' at line 5
where the sql for this looks like that:
CREATE TABLE `the_logs` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`timestamp` INT( 11 ) NOT NULL ,
...
Any idea if i'm doing this wrong using prepare? i've also tried query.. didn't work either.
Any help is greatly appreciated.
T
I expect whichever library you're using to talk to MySQL wants to handle one statement at a time and you're passing it several statements at once.
Try splitting the string on semicolons and executing each statement individually.