简体   繁体   English

查询视图时列名更改大小写

[英]Column names change case when querying view

The following test case describes my problem.以下测试用例描述了我的问题。

CREATE TABLE the_table (id INT, Title CHAR(10));
CREATE VIEW the_view AS SELECT * FROM the_table;
INSERT INTO the_table VALUES (1, 'Hello');

notice that 'Title' in capitalized请注意大写的“标题”

Now when I try:现在当我尝试:

SELECT id, title FROM the_table;

Result:
+------+-------+
| id   | title |
+------+-------+
|    1 | Hello |
+------+-------+

(notice how 'title' is lowercase in both the query and the result column) (注意查询和结果列中的“标题”是如何小写的)

BUT, when i do the same thing on the view:但是,当我在视图上做同样的事情时:

SELECT id, title FROM the_view;

Result: 
+------+-------+
| id   | Title |
+------+-------+
|    1 | Hello |
+------+-------+

It is the same select clause, but this time the column name for 'Title' is Capitalized!它是相同的选择子句,但这次“标题”的列名称大写!

I am trying to reuse a code with a view of the exact same schema, but this messes it up.我试图重用具有完全相同架构视图的代码,但这把它搞砸了。

I can't really find any reference whether this is standard MySQL behavior or not, but is there a way around it?无论这是否是标准的 MySQL 行为,我真的找不到任何参考,但是有没有办法解决它?

Unless you quote the column or table names, SQL is supposed to be case insensitive.除非您引用列名或表名,否则 SQL 应该不区分大小写。 Don't put quotes around you column and table names and you should not have problems.不要在列名和表名周围加上引号,您应该不会有问题。 I have noticed some tools tend to quote the column and variable names.我注意到一些工具倾向于引用列名和变量名。

This is standard MySQL behavior.这是标准的 MySQL 行为。 MySQL uses those column names in the resultset that you use in the main query. MySQL 在结果集中使用您在主查询中使用的那些列名。 Since column names are not case-sensitive in MySQL, it allows querying columns in whatever letter-case and use the same letter-case in the resultset.由于列名在 MySQL 中不区分大小写,因此它允许以任何字母大小写查询列并在结果集中使用相同的字母大小写。

Therefore, SELECT id, title FROM the_table;因此, SELECT id, title FROM the_table; uses "id" and "title" in the resultset.在结果集中使用“id”和“title”。 Try running SELECT ID, TITLE FROM the_table;尝试运行SELECT ID, TITLE FROM the_table; or SELECT Id, Title FROM the_table;SELECT Id, Title FROM the_table; and you'll see column names in respective cases.并且您将在相应情况下看到列名称。

With views, it uses the column names that are provided in the SELECT statement used when creating the view.对于视图,它使用在创建视图时使用的 SELECT 语句中提供的列名。 In your case, it is SELECT * FROM the_table;在你的情况下,它是SELECT * FROM the_table; . . Hence when you run SELECT id, title FROM the_view;因此,当您运行SELECT id, title FROM the_view; , it gives the column names as "id" and "Title". ,它将列名称指定为“id”和“Title”。

However, if you create your view as CREATE VIEW the_view AS SELECT id, title FROM the_table;但是,如果您将视图创建为CREATE VIEW the_view AS SELECT id, title FROM the_table; , using lowercase column names, you will see that the SELECT from view will also return the columns as "id" and "title". ,使用小写列名,您将看到 SELECT from view 也将列返回为“id”和“title”。

I'm not familiar with the MySQL internals but I hope it makes sense.我不熟悉 MySQL 内部结构,但我希望它是有道理的。

If we query how the view is stored:如果我们查询视图的存储方式:

SHOW CREATE VIEW the_view

... we find a result that contains this: ...我们找到一个包含以下内容的结果:

CREATE ALGORITHM=UNDEFINED DEFINER=`test`@`%` SQL SECURITY DEFINER VIEW `the_view` AS select `the_table`.`id` AS `id`,`the_table`.`Title` AS `Title` from `the_table`

This happens as well if you replace * with a column list: MySQL adds an AS clause with an alias, which effectively hard-codes column names.如果用列列表替换*也会发生这种情况:MySQL 添加了一个带有别名的AS子句,这有效地对列名进行了硬编码。

The conclusion is that a view is not a table.结论是视图不是表。 You'll need to create it with the exact case you want to use.您需要使用要使用的确切大小写来创建它。 Whatever, case is normally irrelevant unless your client language messes things up when case changes (eg PHP associative arrays).不管怎样,case 通常是无关紧要的,除非你的客户端语言在 case 改变时把事情搞砸了(例如 PHP 关联数组)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM