数据库级别的别名列名 [MySQL] [英] Alias Column Names at the Database Level [MySQL]

查看:54
本文介绍了数据库级别的别名列名 [MySQL]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

别名"可能是错误的词,因为它用于将列/表名称作为查询中的其他内容引用的上下文中.

"Alias" is probably the wrong word, since that's used in the context of referencing column/table names as something else in a Query.

我感兴趣的是是否有一种方法可以为数据库中的列指定两个名称.如果我要打印这样的表格,它看起来像这样:

What I'm interested in is if there's a way to give a column two names in the database. If I were to print such a table, it would look like this:

mysql> SELECT * FROM User;
+--------------------------+-----------------+
| id | username | uname    | password | pswd |
+----+---------------------+-----------------+
|  0 | bob_jones@gmail.com |    some_pw_hash | 
|  1 | sue_smith@gmail.com |    some_pw_hash |
+--------------------------------------------+ 

在这种情况下,usernameuname 在查询语句中将是同义词,passwordpswd 也是同义词.

In this case, username and uname would be synonymous in query statements, as would password and pswd.

因此以下语句将具有相同的输出:

So the following statements would have the same output:

mysql> SELECT id, username FROM User;
...
mysql> SELECT id, uname FROM User;
...

我想避免做类似的事情

mysql> SELECT id, username AS uname FROM User;

那么,是否存在这样的功能?

So, does a feature like this exist?

干杯,尼尔

推荐答案

不,这是不可能的.为此,您必须添加一个新的、实际的第二列并使用触发器使它们保持同步,这很愚蠢.

No, this is not possible. To do so, you'd have to add a new, actual second column and use triggers to keep them in sync, which would be silly.

只需正确编写 SQL 即可使用正确的列名.

Just write your SQL properly to use the proper column names.

这篇关于数据库级别的别名列名 [MySQL]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆