首页 > 解决方案 > issue in MySQL query due Table name 'CASE'

问题描述

I have installed a script from code canyon and that is having a table with the name of Case.

When I try to execute a query

SELECT COUNT(*) as total FROM case

it shows the following error

Static analysis:

1 errors were found during analysis.

Unexpected end of CASE expression (near "" at position 0)
SQL query: Documentation

SELECT COUNT(*) as total FROM case

MySQL said: Documentation

#1064 - 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 'case' at line 1

I know the case is a reserved word for MYSQL but now How I can resolve this because I can't change the table name and cant modify it into whole script.

标签: mysqlsqldatabasedatabase-designphpmyadmin

解决方案


case is a really poor name for a table or column name. It is obviously a SQL keyword. But, if you can't fix it, use backticks:

select count(*)
from `CASE`

That escapes the name and tells MySQL to treat it as the name of something, rather than the keyword.

I would encourage you to figure out how the database can be fixed. I generally name tables in the plural -- cases for instance. First, they are plural because they almost always have more than one row. A side effect is that plurals conflict with many fewer keywords.


推荐阅读