Home >Database >Mysql Tutorial >How Do I Select Columns with Spaces in Their Names in MySQL?

How Do I Select Columns with Spaces in Their Names in MySQL?

Barbara Streisand
Barbara StreisandOriginal
2025-01-16 19:31:09639browse

How Do I Select Columns with Spaces in Their Names in MySQL?

Using quotes in MySQL to select column names containing spaces

Having trouble selecting column names containing spaces in MySQL? This is a common dilemma for developers. Let’s delve deeper into the problem and find a practical solution.

Question

When you execute a SELECT statement with a column name that contains spaces (such as "Business Name"), MySQL may return an error stating that the column does not exist. This is because MySQL interprets spaces as delimiters, splitting column names into two parts.

Solution

To fix this issue you need to quote the column names correctly. Quotes indicate to MySQL that the entire string within the quotes (including spaces) is the column name.

<code class="language-sql">SELECT `Business Name` FROM annoying_table;</code>

Why do you need to quote?

It is generally recommended to avoid using spaces in column names to prevent this type of problem. However, if you inherit a database with less-than-ideal names, references are critical to accessing those columns.

Many database management systems, such as Microsoft Access, provide graphical user interfaces that make it easy to create tables and columns without having to consider such nuances. This can lead to naming inconsistencies that can cause trouble during program access.

The above is the detailed content of How Do I Select Columns with Spaces in Their Names in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn