SQL exception with 'from' as a column name - mysql

I have a table with a column named 'from'. I want to retrieve data from it and so I tried following query.
select title,from,grade from localcourses where title='new';
But I get following exception due to the column name 'from'.
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,grade from localcourses where title='new'
How can I avoid this without renaming the column name? Thank you.

Try --
select `title`,`from`,`grade` from localcourses where `title`='new';

If you are running MySQL in standard (ANSI) mode, use double quotes to "escape" the keyword:
select title,
"from",
grade
from localcourses
where title='new';
If you are running MySQL in non-standard mode (which is still the default if I'm not mistaken), you need to use MySQL's dreaded "backticks:
select title,
`from`,
grade
from localcourses
where title='new';

On MySQL you can use the ` (back apostrophe -- to the left of the 1 key on your keyboard). Use
`from`.

I'll be the first to say it - you should avoid naming tables, columns, triggers, procedures, functions, etc with the names of reserved, action, and other commonly used words in sql and database engine syntax. It only creates confusion such is the case here.

Assuming Oracle try
select title,"from",grade from localcourses where title='new';

In mySQL, you need to enclose the from column in backtick character
select title,`from`,grade from localcourses where title='new'
I suspect the backtick character you are using is not the right one, I am not sure what type of keyboard you have, so it might not send the proper character in.
Try this instead.
select title,localcourses.from,grade from localcourses where title='new'
and see if that helps

Related

Inserting unix time and special symbol

Trying to run this query -
INSERT INTO rmedvedeva993#gmail.com (url,unix)
VALUES (#https://youtu.be/xXsuqrhD8pw,#1500152563.66077);
after reading about this issue tried wrapping database like this- rmedvedeva993#gmail.com
getting an 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 '://youtu.be/xXsuqrhD8pw,#1500152563.66077)' at line 1
not quite sure what's the issue here,
P.S.: my columns are formated as char(255)
`
#hhttps://youtu.be/xXsuqrhD8pw and #1500152563.66077 aren't valid.
Neither is an email address as the name of a table. If you MUST use an email address as a table name, enclose it in backticks. But think long and hard about why you're doing that, then don't do it.
You probably want VALUES ('https://youtu.be/xXsuqrhD8pw',1500152563.66077); .
The # symbol in MySQL's dialect of structured query language denotes a user-defined variable. So you could have this:
#url := 'https://youtu.be/xXsuqrhD8pw';
#ts := 1500152563.66077;
INSERT INTO table (url,unix) VALUES (#url,#ts);
You need to quote your strings (whether char(xx), varchar(xx), or any other type which is represented as a string); and, when the names of your tables are not just letters and numbers, you have to quote them with the backtick quote: `. You most probably don't won't either the # symbol.
INSERT INTO `rmedvedeva993#gmail.com` (url,unix)
VALUES ('https://youtu.be/xXsuqrhD8pw','1500152563.66077');
Side Note: Is your table really named rmedvedeva993#gmail.com? Can you post your table definitions (use SHOW CREATE TABLE table_name).

Column alias is not working for Group, Where and Having in MySQL

I am having the following problem.
I want to execute as sql statement that filters the results with HAVING. However the having is on a column that is calculated from an IF() function inside the select. This way, MySQL server complains that the column inside having clause is unknown!
EX:
SELECT col1,col2,IF(expr1,expr2,expr3) AS `wantedColumn`
FROM....
WHERE ...
HAVING LENGTH(`wantedColumn)>0
It is as mysql cannot understand that the column returned by the if expression is named wantedColumn... If I use other columns it is working correctly. But I need to filter on that. Any suggestions?
Thanks
This is due to mode configurations:
If your database is using the mode 'ONLY_FULL_GROUP_BY', you will not be able to use a column alias in WHERE, GROUP BY or HAVING, it does not work. You have to either repeat the whole expression or use sub query.
In order to know if you are using 'ONLY_FULL GROUP_BY' mode, use the following query:
SELECT ##sql_mode;
If you want to change it to a mode that allows it:
SET SESSION sql_mode =STRICT_TRANS_TABLES;
To know more about SQL modes:
http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
Answer by comment from Inanda Menezes #Inanda:
Look at it: sqlfiddle.com/#!2/5294e4/6 , I just removed the backticks
from the alias used inside the having and your example worked.
According to mysql documentation you don't need to use backticks on
identifiers that does not have special words or characters. If you use
it, in most of cases, it will not cause your query to fail, but it's
not necessary at all to quote normal identifiers with non-standard
escape. Anyway, it seems to fail when escaping with backsticks a alias
in having clause which uses length function, so just don't escape this
one. – Inanda Menezes

when i will run this query it will give an 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 'specific) VALUES ('5.jpg','kids','anyone')' at line 1,,
My query is
$sql="INSERT INTO imagetable(image,name,specific) VALUES ('$dbimage','$dbname','dbspec')";
specific is a reserved word in MySQL. Either use backticks to escape it or use another name for your column.
INSERT INTO imagetable (image, name, `specific`)
VALUES ('$dbimage','$dbname','dbspec')
sql="INSERT INTO `imagetable` (`image`,`name`,`specific`) VALUES ('$dbimage','$dbname','dbspec')";
While not required, it is a good practice to surround your column names (and table names) with ` characters. This avoids issues with reserved words used by the SQL language.
The reason you are getting this issue is because "specific" is a reserved keyword by the SQL language. Think of it like trying to name a variable "if". Since the keyword "if" is reserved by the coding language, you cannot do this. It is the same concept with "specific" in SQL.

# symbol messing up SQL queries

I must use a SQL SELECT statement to return some results. I need to return two pieces of information regarding employees, the Employee# and EmployeeName.
I've tried
SELECT Employee#, EmployeeName FROM EmployeeTable
But in MySQL everything from the # symbol forward is greyed out. That symbol is messing up my query but it's a part of the table given me. How do I search using this Employee# without it getting messed up? Thank you.
Use backticks (`) to escape weird column and table names in MySQL:
SELECT `Employee#`, EmployeeName FROM EmployeeTable
Or, even better, don't use special characters in those names in the first place.
For MySQL:
SELECT `Employee#`, EmployeeName FROM EmployeeTable
For SQL Server:
SELECT [Employee#], [EmployeeName] FROM [EmployeeTable]
It's the same idea, but MySQL has a different delimiter for escaping table names.
SELECT [Employee#], EmployeeName FROM EmployeeTable
The ANSI SQL compliant way to escape column names is to enclose column names in double qoutes .
This works across all ANSI compliant DBMS - MySQL ,SQL Server,Oracle etc.
For MySQL , you can use backticks or enable ANSI SQL mode after which
SELECT "Employee#", EmployeeName FROM EmployeeTable

Error in MySQL Query (Banned Word?)

I have an MySQL query, which returns an error message. I think it could be due to the word "out". Normally, I would just change the field name but I am working on some software that I am not used to and I don't know how much of a change that would be. So, I want to be sure if I have to.
Here is the query:
SELECT * FROM probid_bids WHERE auctionid=73 AND out=0 AND invalid=0
Here the 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 'out=0 AND invalid=0' at line 1
OUT is indeed a reserved word. You can encase the column names in backticks to quote the names, and thus avoid this problem, like so:
SELECT * FROM probid_bids WHERE `auctionid`=73 AND `out`=0 AND `invalid`=0
OUT is a reserved word (it is used to specify the type of parameters -- IN, OUT, INOUT -- when creating procedures). Try enclosing it inside backticks (`).
The rules regarding how and when to quote the identifiers (table names, column names, etc) are described here.
Note: certain MySQL configurations allow you to use double quotes as well but this should be avoided; stick with using backticks to quote identifiers and single quotes to quote strings.
Escape the keys:
SELECT * FROM `probid_bids` WHERE `auctionid`=73 AND `out`=0 AND `invalid`=0