Error 1054 - Unknown column in where clause [duplicate] - mysql

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
I don't know why I can't use where clause in this project, it shows up throwing Error Code: 1054.
in this case :Error Code: 1054. Unknown column '15/05/20' in 'where clause'
select * from covid_19_india;
select `state/unionterritory`,
cured, cured, confirmed,
round(deaths/confirmed)*100 as mortality_rate
from covid_19_india
where date = `15/05/20`;
Here is the file I have imported data from the below file...

try the following, put date between ' ' as single quote is the delimiter for the string and it denotes textual data.
Backticks and regular quotes have different meanings in SQL. Quotes (single or double) indicate a literal string, whereas backticks are quoted identifiers.
select `state/unionterritory`,
cured, cured, confirmed,
round(deaths/confirmed)*100 as mortality_rate
from covid_19_india
where date = '15/05/20';

You use backticks ` to quote your 15/05/20, and as of that it becomes an identifier, and is not handled as value. So the database is looking for the column with the name 15/05/20 (like with your state/unionterritory column)
You need to change the quoted to ' (where date = '15/05/20')

Related

MySQL "Unexpected token" error while updating [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 days ago.
I am running this query to update a password field on user with email x#mail.com and want to update his password, but I am having error.
The mysql query:
UPDATE users
SET password=myword
WHERE email=x#mail.com;
The password field is an md5 encrypted field.
The error I got is
#1054 - Unknown column 'myword' in 'field list'
This is my table structure:
the mysql table structure
I tried to put the values in "" and '' as well as `` but still the error occurs.
You must use single quotes for string values.
UPDATE users
SET password='myword'
WHERE email='x#mail.com';
Double quotes are used for quoting table and column names with special characters and the like.

Escaping a forward slash in an SQL name? It can be "escaped", but SQL believes it to be multiple columns

The last person in my job has flooded column names with special characters such as (?,!, and /), as well as used many reserved keywords for column names (more often than not, timestamp or user is used).
Normally, I step around this by using double quotes or brackets to escape the SQL object. A subset of the full list of columns are below:
DriverID,
Department,
Odometer,
MerchantState,
MerchantCity,
gallons/Units,
timestamp,
tax
Inside my query, I wrap the two columns in question (gallons/units and timestamp) inside double quotes. Timestamp because it's a reserved keyword, and Gallons/units, because without the quotes, SQL reads the query, stops at the slash, and tells me "Gallons" is not a column inside the table.
If I do wrap double quotes around the column name, SQL returns a different error: "Operand should contain 1 column(s)".
I've tried every variant (only capturing the slash in quotes, quoting both, using brackets, mixing brackets and quotes, etc. but with to no avail).
Is there anything I can do to fix this query short of renaming the column name and changing the associated code in the program that pulls from it? (the really tedious task I'm trying to avoid).
In SQL Server, identifiers can be delimited using square brackets, e.g.
SELECT [gallons/units] ...
In MySQL, identifiers can be delimited using backticks, e.g.
SELECT `gallons/units` ...
(NOTE: If MySQL SQL_MODE includes ANSI_QUOTES, then double quotes are treated as delimiters for identifiers, similar to the way Oracle handles double quotes; absent that setting, double quotes are handled as delimiters for string literals. With ANSI_QUOTES included SQL_MODE, "gallons/units" will be interpreted as an identifier (column name). Without ANSI_QUOTES, MySQL will see it as a string literal, as if it were enclosed in single quotes.)
FOLLOWUP:
As far as an error "operand should contain only 1 column(s)", that's usually a problem with query semantics, not an issue with escaping identifiers.
A subquery in the SELECT list can return only a single expression, for example, this would throw an error:
Query: SELECT 'foo' , ( SELECT 'fee' AS fee, 'fi' AS fi )
Error Code: 1241
Operand should contain 1 column(s)
You can try backticks instead of double quotes
`gallons/units`
There are a couple of options. First, have you tried using %/ to escape the slash?
Example: "select * from 'gallons%/units';"
Second one I've found, which may or may not be helpful, is to provide an escape character definition, such as
http://blogs.msdn.com/b/zainala/archive/2008/08/17/using-and-escape-clause-in-sql-server-like-query.aspx
select * from MyTable where Description like '|[summary|]%' escape '|'
In your case
select * from 'gallons|/units' escape '|'
You indicate both mysql and sql-server in your tags, so I'm not sure which server support I should be looking for exactly.

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 'FROM [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I get this error at this code:
SELECT "LastUpdate" ;
FROM "xx_yy";
Is LastUpdate a reserved word ?
I tried to change " " to `` or delete them, I don't really know the perfect combination to make it work. I'm beginner in this.
Get rid of the quotes around your column identifier and tablename. That makes them strings instead of identifiers. Either use ticks or nothing at all. Also, ditch the semi-colon after the first line as it is terminating your query before it reaches the FROM clause.
SELECT `LastUpdate`
FROM `xx_yy`;
or
SELECT LastUpdate
FROM xx_yy;
A semicolon (;) signifies the end of a statement. So you actually have two separate, distinct statements:
SELECT "LastUpdate"
FROM xx_yy
The second statement is not valid, which is why you are seeing the error.
Solution: Remove the semicolon at the end of the first line:
SELECT "LastUpdate"
FROM "xx_yy";
Also note if the ANSI_QUOTES sqlmode is not enabled, MySQL treats double-quotes as string literals (the same as single quotes). You may need to change these to the MySQL-specific backtick, or remove them entirely:
SELECT `LastUpdate`
FROM `xx_yy`;
Remove the first semicolon.
SELECT FOO FROM BAR
The above is all one statement.
Most likely your query should look like
SELECT "LastUpdate" FROM "xx_yy";
; is marking an end of a query.

How to access table when the name is a keyword [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 8 years ago.
I just realized that i assigned a table name as "AS" and when i was trying to do a select query, i kept getting an 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 '"as"'.
So i looked up for reserved words and found out that "AS" is reserved. Well, i think i knew before (used for aliases) but just didn't consider it.
So to fix this will easily be to rename the table name. But assuming i don't want to, can i still access this table using some sort of symbol ? i tried putting it in quotes and double quotes but no success.
With mysql, you can wrap reserved words (or any words for that matter) in backticks to cause word to be parsed as a literal name rather than the keyword:
select * from `AS`
Read more about it in the on line documentation under "identifier quote character".
Yes use this(backtiks):
SELECT * FROM `as`
You should use backticks. Else MySQL will consider it as a Keyword. If we use backtick then they are called quoted identifiers and they tell the parser to handle the text between them as a literal string. They are useful for when you have a column or table that contains a keyword or space.
Please refer: http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html

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.