我的一个专栏叫。我不能改名字,因为不是我做的。 我是否允许做一些像SELECT from TableName或有一个特殊的语法,以避免SQL Server混淆?


当前回答

在MySQL中,除了使用反引号('),您还可以使用UI来更改列名。右键单击表>修改表>编辑包含sql关键字>提交的列名。

select [from] from <table>

需要注意的是,上述方法在MySQL中不起作用

其他回答

简单的解决方案

假设列名是from;因此,查询中的列名可以通过表别名引用

Select * from user u where u.from="US"

一些可靠的答案——但投票最多的答案是狭隘的,只涉及SQL Server。总而言之:

If you have source control, the best solution is to stick to the rules, and avoid using reserved words. This list has been around for ages, and covers most of the peculiarities. One tip is that reserved words are rarely plural—so you're usually safe using plural names. Exceptions are DIAGNOSTICS, SCHEMAS, OCTETS, OFFSETS, OPTIONS, VALUES, PARAMETERS, PRIVILEGES and also verb-like words that also appear plural: OVERLAPS, READS, RETURNS, TRANSFORMS. Many of us don't have the luxury of changing the field names. There, you'll need to know the details of the RDBM you're accessing: For SQL Server use [square_braces] around the name. This works in an ODBC connection too. For MySQL use `back_ticks`. Postgres, Oracle and several other RDBMs will apparently allow "double_quotes" to be used.

在表名上加上冒犯性的单词也可以。

如果是在PostgreSQL中,在名字周围使用双引号,比如:

select "from" from "table";

注意:PostgreSQL内部自动将所有不带引号的命令和参数转换为小写。命令和标识符不区分大小写。sEleCt * from tAblE;解释为select * from table;。然而,双引号内的参数是原样使用的,因此是区分大小写的:select * from "table";and select * from "Table";从两个不同的表获取结果。

以下是两种方法:

使用后面的引用如下:

从表名中选择“from”

你可以用表名提到:

SELECT TableName. FROM FROM TableName

从这里的答案和我自己的经验来看。如果您计划实现可移植,唯一可接受的答案是不要对表、列或其他名称使用SQL关键字。

所有这些答案都适用于各种数据库,但显然有很多不支持ANSI解决方案。