Home >Database >Mysql Tutorial >Why Does My Query Fail with 'Column 'Mary' Does Not Exist' Despite the Column Existing?

Why Does My Query Fail with 'Column 'Mary' Does Not Exist' Despite the Column Existing?

Susan Sarandon
Susan SarandonOriginal
2025-01-03 15:45:39520browse

Why Does My Query Fail with

Column 'Mary' Does Not Exist: A Querying Mishap

When executing a query, you may occasionally encounter an error message indicating that a particular column does not exist, even though it's present in your database. One such scenario is when the query references the column name with erroneous characters.

The Case of 'Mary'

In the provided example, the error pertains to the column 'Mary'. However, upon examining the query, it becomes evident that there is no column named 'Mary' in the query. Instead, 'Mary' is intended to be a value assigned to the column 'personname'.

The key takeaway here is that the error arises from the incorrect character encoding of 'Mary'. In the query, 'Mary' is enclosed in smart quotes (‘Mary’), which are Unicode characters. Database systems generally prefer plain single quotes ('Mary'), which are ASCII encoded.

Resolving the Issue

To rectify this issue, simply replace the smart quotes with plain single quotes in the query. Modify your query as follows:

SELECT telephone.telephonenumber as tel
FROM person, telephone
WHERE person.idperson = telephone.idperson
AND person.personname = 'Mary';

By making this slight adjustment, your query will now correctly search for people with the name 'Mary' and retrieve their telephone numbers.

The above is the detailed content of Why Does My Query Fail with 'Column 'Mary' Does Not Exist' Despite the Column Existing?. 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