# symbol messing up SQL queries - mysql

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

Related

Why do all my SQL queries have to be wrapped using the ` symbol?

I have been working on a database for my coursework and have used phpMyAdmin to build it. Now I am working on the queries using the query tool.
When I pick the tables and data I want to query and press "update query" it will generate the query which looks something like this:
SELECT `Customer`.`CustomerName`, `OrderDetails`.`Product`, `OrderDetails`.`QuantityOrdered`
FROM `Customer`
LEFT JOIN `Order` ON `Order`.`Customer` = `Customer`.`CustomerID`
LEFT JOIN `OrderDetails` ON `OrderDetails`.`Order` = `Order`.`OrderID`
This works fine and gives me the results I was expecting. However when I try and write my own query and put something like "SELECT Customer.CustomerName," WITHOUT the ' symbol it won't work and just throws up an error message.
Must I always wrap them using the ' symbol for the query to work?
Forward quotes are used to escape object names in MySQL. You don't have to use them unless you use names that wouldn't be valid identifiers - in this case, the table name order is a reserved word, and must be escaped. All the other tables and columns you're using seem to be OK.
Except for the visual nightmare and ability to create horrendous table names, backticks are entirely unnecessary. You will, however, be required to wrap any variables in single quotes.
As you can see from my example below, using backticks is not a requirement with PHPMYADMIN;
The reason it is not working when you remove the backticks is because you have a column called 'order'. Order is a keyword in SQL and therefore cannot be used as a column name without being wrapped in either quotes or backticks.

sql server equivalent of mysql where in

I have always used a MySQL Where In to filter for multiple values of a single field. I am looking for the equivalent for SQL Server. When I set this up on SS it looks at each item as a field in my table instead of a record in my field. Does anyone know the equivalent? Thanks!
MySQL:
SELECT quote_id,entity_id FROM customer.customer_id4
WHERE
quote_id IN ("00033658.0","00033361.0","00032971.0")
Error when using similar format in SS:
Invalid column name '00033658.0'
Invalid column name '00033361.0'
Invalid column name '00032971.0'
" in SQL server is used to delimit field names. You need to use ' instead, e.g.
... quote_id IN ('00033658.0', '00033361.0', etc...)
^-- ^--- note the different quotes
I think you just have to replace double quotes(") with a single quote (').
SELECT quote_id,entity_id FROM customer.customer_id4
WHERE
quote_id IN ('00033658.0','00033361.0','00032971.0')
Try using single quotes in the IN statement and see this link: http://technet.microsoft.com/en-us/library/ms177682.aspx

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

what does back tick do in mysql statements?

In a statement like this;
$sql = "SELECT distinct `term`,count(*) as count
FROM {$temp_table_name}
group by `term` order by count DESC";
What does using the back tick character (`) around the field name 'term' buy me?
Is the usage of back ticks for performance reasons? Or is it for some sort of a SQL injection protection?
Note: After I submit the question, I realized that the backtick character does not show around the field name 'term' - right here on stackoverflow.
I don't know of a way of making it appear here in the question body.
If term is mysql key word, you need to quote it by `, otherwise, it is not necessary.
Ps: distinct is not necessary in your case, because you group by it.
The back-tick is the 'official' identifier quote character.
http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
It allows a wider array of characters in an identifier, as described on the linked documentation.
Backticks just allow the use of spaces or other alternate characters in field names.
I think it's already been pretty well explained here.
When We use a keyword as a table name,field-name in MySQL use backticks, or double-quotes when ANSI_QUOTES is enabled.Other wise it is not necessary.It is not releated to SQL injection protection

SQL exception with 'from' as a column name

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