MySQL throws an error when receiving my query [duplicate] - mysql

This question already has an answer here:
How to escape value keyword in mysql while using Select statement
(1 answer)
Closed 3 years ago.
I have to access a MySQL database that looks like this:
LOG_ID KEY TARGET CREATOR
1 okaytest 297d09d5-55fe faec09c0-159e
I can do the following query:
SELECT * FROM DATABASE WHERE LOG_ID=1
This would return me the column correctly.
But I can't do the following query.
SELECT * FROM DATABASE WHERE KEY='okaytest'
I believe that there might be a problem with the word KEY being a reserved keyword in MySQL, but I have to access that specific database, I can't change it's name and I must select it from the key

try like below by using the backtick `
SELECT * FROM DATABASE WHERE `KEY`='okaytest'
actually for reserve word you have to use this backtick otherwise it will thorow error here is the reserve word list
another options better not to use reserve word or incase of necessary you have use it by using backtick

Related

why won't mySQL let me SET return=1? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
It makes no sense. I have a TINYINT field that is either 0 or 1 and I'm trying to set it like this:
UPDATE db_products SET return='1' WHERE product_id=342343434
But it tells me there's a mySQL syntax error. What am I doing wrong here? Is the problem that the field is called return? If so, how do I get around it without changing it?
In most cases, you can get around reserved word problems by enclosing with "ticks"; the quote-like thing that usually shares the ~ key.
UPDATE db_products SET `return`='1' WHERE product_id=342343434

MYSQL - Set/Update Syntax [duplicate]

This question already has answers here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I have a table like this:
Why is this command not working:
UPDATE 'stitch' SET 'claim-time'='20' WHERE 'group'='010000'
I get the 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 ''stitch' SET claim-time='20' WHERE group='010000'' at line 1
Everything in the table is text.
group is a reserved keyword in mysql so use backticks to escape it
`group`
Also you are selecting the string as column name, correct format is
UPDATE `stitch` SET `claim-time`='20' WHERE `group`='010000'
Try removing the single quotes from stitch, claim-time and group. Either leave them out or use backquote `. The comma is used for strings, not table and field names.
Also, I don't know what data type claim-time and group are. If they are numeric (int, bigint, etc) and not string (varchar, text, etc) then you'll need to remove the single quotes from those too.
update stitch set claim-time=20 where group='0100000'; # assuming group is a string data type
Try this.
UPDATE TableName SET claim-time='20' WHERE group='010000';
That is considering if claim-time is a varchar datatype. If it's a number just remove the quotes.
Remember to avoid reserved names such as field names such as name, password, group, user and stuff just to be safe. Make it user1, group1 instead or something like these.

Retrving data from MySql with case sensitivity [duplicate]

This question already has answers here:
How can I make SQL case sensitive string comparison on MySQL?
(12 answers)
Closed 8 years ago.
I have a Login table in MySql database . . In table there is a column by cname and one of the value is 'Raghu'. My question is when i write the query as
Select *from Login where cname='raghu';
Then it is retrieving the record which contains 'Raghu' . I want it to retrieve according to case . How can I retrieve with case sensitively, values in the data of tables.
Use: 10.1.7.7. The BINARY Operator
The BINARY operator casts the string following it to a binary string. This is an easy way to force a comparison to be done byte by byte rather than character by character.
Select * from Login where binary cname='raghu';
SELECT * from Login WHERE STRCMP(cname,'Raghu')=0;
Can you try this, you can use LOWER FUNCTION in either column name LOWER('cname') or in value LOWER('raghu');
Select *from Login where LOWER(`cname`) = 'raghu';

Grave accents instead of '' in mysqli_query [duplicate]

This question already has answers here:
Do different databases use different name quote?
(3 answers)
Closed 9 years ago.
I have a problem, I looked for it on google, couldn't find it.
$result = mysqli_query($link,"SELECT * FROM update ");
Error
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
When I change the query to:
$result = mysqli_query($link,"SELECT * FROM `update` ");
It works. Why is that?
update is a reserved word in MySQL.
You should always use backticks
`
in `table` and `column` names to avoid errors like that.
List of Reserved Words
Those are not "grave accents" (accents go above letters); they are backticks.
You're supposed to surround field names with backticks, to show that they are field names and not functions, operators, commands, etc.
You can usually get away without doing that (and it would seem that you are quite used to it!), but in the case that your names are in fact MySQL reserved keywords — for example, UPDATE — you can't.
UPDATE is a keyword for MySQL, so it cause an error (mysql is pretty confused because you're starting update inside select). Basically its recommended to put database, table and column names between `` to avoid such situations, also you should check what mysqli_query did return, in this case you're getting false instead of mysqli_result object and you can read an error message (probably not saying much more than 'you have an error near... check your query syntax', but anyway you would know that there is something wrong ;)) :
if (!($result = mysqli_query($link, $query))) {
die('MySQLi error: ' . mysqli_error($link));
}
I'm no SQL expert, but update is a special word which SQL actively looks for. Therefore, the select stuff in a table named update, you need to have update in quotes.

phpmyadmin sql apostrophe not working [duplicate]

This question already has answers here:
character for single quote
(1 answer)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
hey guys was hoping you could help me out,
Not sure if I always had this problem or if its new, in phpmyadmin in the sql tab, the apostrophe it recognizes is different from what i type, for example,
when i type, it is
SELECT * FROM 'table'
this gives me an error, so instead I have to like copy/paste the inverted commas of some prebuilt query so that it looks like
SELECT * FROM `table`
see how the apostrophes are different? any way I can fix this?
also, i have seen many queries on the web, and i think even queries i call from php dont require table names to have apostrophes. But when write it in phpmyadmin, I can do queries without table names having apostrophes?
thanks in advance.
In MYSQL, table is a reserved keyword. If you want to use reserved keywords in mysql in query, you have to enclose them in backtick(`).
As table is reserved keyword you query should be
SELECT * FROM `table`
Regarding single quote ('), in mysql, it represents string value.
SELECT *, 'table' FROM `table`;
Demo
You should only need to quote table names when they conflict with a reserved word.
Also:
` = Grave accent, or (because someone needed to invent a word) backtick
' = Apostrophe, or straight single quote
You dont need apostrophe on table name.
You should use ` in cases that your table/field name is a reserve word eg:
SELECT `distinct`, myfields FROM mytable
note that distinct is an sql command so you need to put the `.
SELECT * FROM `table`
table here should be inside `.
There are two different characters, the backtick and the single quote. Table and column names can be surrounded by the backtick, strings can be surrounded by quotes. There is nothign to fix :D