SQL: Unsual names - mysql

I'm trying to select some information which is inside a table with the name online-players using Left join in SQL however, ever time I run the code it just returns the following error.
"Unknown column 'ontime' in 'field list'"
I am assuming its because of the - in the name of the table that course the error.
Is there anyway of going around it so it still gets the information?

That's not true, that error will be generated only when the corresponding field name doesn't exist. However you can enclose field and table names with backticks to be sure

Backticks are required, that's all.

You can use backticks (`) to refer to the table.
select * from `online-players`
This is provided that you don't have a typo in your query and such.

Related

Error when adding column with numeric name

I currently have a MariaDB database with columns named after dates : 20200105, 20200914 etc.
If I try to add a column using ALTER TABLE dates ADD COLUMN IF NOT EXISTS (test VARCHAR(255));, it works and the test column is created.
If I type ALTER TABLE dates ADD COLUMN IF NOT EXISTS (20201205 VARCHAR(255));, though (so, with a number replacing "test"), the creation does not work anymore and MariaDB tells me that there is an error with my SQL syntax.
I have tried to put quotes around the column name, but that does not work (not even with "test").
Is there something obvious I am missing ?
Use backticks to escape the column name:
ALTER TABLE dates ADD COLUMN IF NOT EXISTS (`20201205` VARCHAR(255));
But really best practice frowns upon the use of naming your database objects with mandatory backticks. The reason for using a name like 20201205 as a column name is that you will forever be needing to escape it using backticks. Also, from a data design point of view, your data should grow with new dates in terms of increasing the number of records, not columns.

changing column name in mysql from sum(xyz) to xyz

In MySQL, how do I change the column name of a table from "sum(xyz)", to say "xyz"? I have tried the following solutions for just changing the column name:
Change Column Name in MySQL
Rename column SQL Server 2008
However, it always throws up an error saying that the syntax is not right. I feel that it is because of the sum() function, because it doesn't allow me to use SELECT on that column too (when done separately).
Is there any way past this? A solution to access the values in that column without changing the column header is also appreciated!
I am using WAMPSERVER to run MySQL version 5.1.53.
Thanks
So, your column name is "sum(xyz)" and you can't do a select because of the "sum()" function.
Try
SELECT ` sum(xyz)` as xyz from mytable;
Try this:
ALTER TABLE mytable CHANGE COLUMN `sum(xyz)` `xyz` <yourdatatype>;
Use AS to make virtual field
Your code will be same as SELECT SUM(yourField) as xyz FROM table

syntax error when trying to alter table column name mysql

I am working on a homework assignment. my instructions are this:
In the MATCHES table, change the column name MATCHNO to MATCH.
my syntax i wrote will be below, the problem I am having is with the word "match"
its giving me a syntax error of unexpected syntax error for it. I am assuming
that match is a keyword of some sort but after googling I'm not sure exactly.
the database I am using is the popular "tennis" one for teachings.
here is my code:
sql
ALTER TABLE MATCHES
CHANGE MATCHNO MATCH INT;
what can I do to fix this? If i change the name of "match" to something else it works but my instructions say to use "match"... is this possible?
The problem is that your query contains MySQL keywords (MATCH, cf. https://dev.mysql.com/doc/refman/5.5/en/keywords.html).
Put the table and column names in backticks (`) in order to prevent MySQL from interpreting those as keywords.
ALTER TABLE `MATCHES` CHANGE `MATCHNO` `MATCH` INT;

Column alias with leading space causes error: Unknown column 'BAR. COL1' in 'field list'

I am working on a utility that exposes data from a warehouse that has machine-generated table/column names. The user is allowed to specify new names for the tables/columns to make the results more meaningful. The alias can be any valid MySQL identifier name but I have found an issue with column names that contain a leading space. Now I know that would be a foolish column name but I do not wish to impose any arbitrary limit on the user just because I was lazy and couldn't find the correct way to do something.
This simple SQL statement reproduces the problem:
SELECT ` COL1` FROM (SELECT 1 AS ` COL1`) BAR
Error Code: 1054. Unknown column ' COL1' in 'field list'
Am I missing something? I am using MySQL 5.6.11.
Can you retrieve the table structure? Wanted to see the actual column names of the table BAR.
MSSQL with trim out any spaces in the column name when you run the query you have above.

How to rename a MySQL column from long to something else

I have a database table with a column name as 'long'. It is a keyword of MySQL and not sure whether it is a keyword conflict or something, I cannot run some of the simple SQL script using this keyword-column name. I try to run the SQL to alter the column name, using: alter table mytable change column long lng double but it doesn't work.
The question is, what's the reason from prevent the above simple alter SQL from working? (For any other columns on hand, the above SQL works) and how can I make it work if it is a reason of keyword conflict? Is there any keyword escape symbol that I can use in MySQL?
Thanks very much.
Tried:
ALTER TABLE mytable CHANGE COLUMN `long` lng DOUBLE
(The backtick is, AFAIK, MySQL specific.)
An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. ... The identifier quote character is the backtick ("`") (source)
Try using a "right quote" around "long"
Like this :
`long`
try again query working properly alter table table_name change column long lng double but its neccessary the datatype of long should be integer then try may be your problem solved