从 MySQL 表生成报告
假设您有一堆 MySQL 表,并且您希望最终用户能够使用 PHP 脚本使用这些数据生成报告。 您可以在下拉列表中显示这些表中的字段名称,这样用户就可以说“first_name
equals John”。 伟大的。 但是如果您希望这些字段名称更具可读性怎么办? 例如,我希望用户能够选择“第一个宠物的名称”作为字段,而不是“first_pet_name
”。 我绝对不想将该信息存储在标记中,因为我们可能会非常频繁地添加和删除表。 实现这一目标的最简单方法是什么?
Let's say you have a bunch of MySQL tables, and you want your end users to be able to generate reports with that data with a PHP script. You can present the field names from those tables in a dropdown, so a user might be able to say, "first_name
equals John." Great. But what if you want those field names to be a little more readable? For instance, I'd like the user to be able select "Name of First Pet" as a field, instead of "first_pet_name
." I definitely don't want to store that information in the markup, as we might be adding and removing tables pretty frequently. What's the simplest way to pull this off?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会做一些简单的事情,比如用空格替换下划线,并将第一个字母大写。
I'd do something simple like replacing underscores with spaces, and upper casing the first letter.
您可以创建一个表,将文本信息绑定到表+列对。 假设您有一个表users,并且您想要显示列名称“Name Of First Pet”而不是first_name。
假设(表名称ColumnTextInformations):
因此您的列标签拥有唯一的标识符。
使用它非常简单:
You can create a table, to bind text informations to table+column pairs. Let's say you have a table users, and you want to show column name 'Name Of First Pet' instead of first_name.
Let's say (table name ColumnTextInformations):
So you have unique identifiers for your column labels.
Using it it's very easy:
我会将其存储在数据库中。
其中 schema 就是您在 mysql 中通常所说的“数据库”(切换数据库时
USE
之后的内容); 表格和列非常明显。当然,您必须确保 DBA 在更改架构时进行更新。
我相信 MySQL 允许对表进行注释,但不允许对列进行注释,或者您可以使用它。
编辑:将 varchar 更改为 64,因为这是 MySQL 手册记录的最大大小。 此外,事实证明,如果您愿意,您可以在每一列上添加注释 - 并且您可以从 information_schema.columns 中读回这些注释。 但我仍然会按照上面所示的方式进行操作,因为它更灵活(您可以轻松地在其中添加其他数据,例如“我应该显示此字段”标志),并且还允许将注释用于其预期目的。
I'd store it in the database.
Where schema is what you commonly call "database" in mysql (what goes after
USE
when you switch databases); and table and column are pretty obvious.Of course, you'll have to make sure the DBA updates that whenever changing the schema.
I believe MySQL allows a comment on a table, but not a column, or you could use that.
Edit: Changed the varchar to 64 because that's what the MySQL manual documents as the max size. Also, it turns out you can put a comment on each column if you wish — and you can read those back from information_schema.columns. But I'd still do it the way shown above as its more flexible (you can put additional data in there easily, such as your "should I show this field" flag) and also allows comments to be used for their intended purpose.
最简单的解决方案是使用分隔标识符。 这允许您拼写包含空格、标点符号和特殊字符、SQL 关键字等的表名和列名。
您必须在 SQL 查询中分隔表名和列名(包括使用的
CREATE TABLE
语句)来定义它们)。默认情况下,MySQL 使用反引号(如上所示)作为分隔标识符。
请参阅“不同的数据库是否使用不同的名称引用”更多细节。
The simplest solution is to use delimited identifiers. This allows you to spell table names and column names containing whitespace, punctuation and special characters, SQL keywords, etc.
You have to delimit the table names and column names in SQL queries (including the
CREATE TABLE
statements used to define them).MySQL uses back-ticks (as shown above) for delimited identifiers by default.
See "Do different databases use different name quote" for more details.
我并不是说这是一个好主意,但从技术上来说 - 技术上 - 如果您在整个代码中用反引号将这些标识符括起来,MySQL 将允许您创建包含空格的表和列。 例如:
I'm not saying this is a good idea, but technically -- technically -- MySQL will let you create tables and columns that include spaces if you surround those identifiers with backticks throughout your code. For example:
听说过列别名吗?
选择
pet AS
第一个宠物的名字
,user_name AS
用户名称
来自用户
WHERE 用户 = '比尔';
Ever heared of column aliases ?
SELECT
pet AS
Name of First Pet
,user_name AS
Name of User
FROM users
WHERE user = 'Bill';