从 MySQL 表生成报告

发布于 2024-07-16 02:33:06 字数 269 浏览 7 评论 0原文

假设您有一堆 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

朱染 2024-07-23 02:33:07

我会做一些简单的事情,比如用空格替换下划线,并将第一个字母大写。

I'd do something simple like replacing underscores with spaces, and upper casing the first letter.

再见回来 2024-07-23 02:33:07

您可以创建一个表,将文本信息绑定到表+列对。 假设您有一个表users,并且您想要显示列名称“Name Of First Pet”而不是first_name

假设(表名称​​ColumnTextInformations):

ParentTable | ParentColumn | Text
      users |   first_name | Name of First Pet
...

因此您的列标签拥有唯一的标识符。

使用它非常简单:

SELECT Text 
FROM ColumnTextInformations
WHERE parentTable = ? AND parentColumn = ?

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):

ParentTable | ParentColumn | Text
      users |   first_name | Name of First Pet
...

So you have unique identifiers for your column labels.

Using it it's very easy:

SELECT Text 
FROM ColumnTextInformations
WHERE parentTable = ? AND parentColumn = ?
爱*していゐ 2024-07-23 02:33:07

我会将其存储在数据库中。

CREATE TABLE human_labels (
    schema  varchar(64) not null,
    table   varchar(64) not null,
    column  varchar(64) not null,
    label   tinytext    not null,
    primary key (schema, table, column)
);

其中 schema 就是您在 mysql 中通常所说的“数据库”(切换数据库时 USE 之后的内容); 表格和列非常明显。

当然,您必须确保 DBA 在更改架构时进行更新。

我相信 MySQL 允许对表进行注释,但不允许对列进行注释,或者您可以使用它。

编辑:将 varchar 更改为 64,因为这是 MySQL 手册记录的最大大小。 此外,事实证明,如果您愿意,您可以在每一列上添加注释 - 并且您可以从 information_schema.columns 中读回这些注释。 但我仍然会按照上面所示的方式进行操作,因为它更灵活(您可以轻松地在其中添加其他数据,例如“我应该显示此字段”标志),并且还允许将注释用于其预期目的。

I'd store it in the database.

CREATE TABLE human_labels (
    schema  varchar(64) not null,
    table   varchar(64) not null,
    column  varchar(64) not null,
    label   tinytext    not null,
    primary key (schema, table, column)
);

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.

笨死的猪 2024-07-23 02:33:07

最简单的解决方案是使用分隔标识符。 这允许您拼写包含空格、标点符号和特殊字符、SQL 关键字等的表名和列名。

您必须在 SQL 查询中分隔表名和列名(包括使用的 CREATE TABLE 语句)来定义它们)。

CREATE TABLE `User Facts` (
  `User Name`         VARCHAR(100)
  ...
  `Name of First Pet` VARCHAR(100)
  ...
);

SELECT `Name of First Pet`
FROM `User Facts`
WHERE `User Name` = 'Bill';

默认情况下,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).

CREATE TABLE `User Facts` (
  `User Name`         VARCHAR(100)
  ...
  `Name of First Pet` VARCHAR(100)
  ...
);

SELECT `Name of First Pet`
FROM `User Facts`
WHERE `User Name` = 'Bill';

MySQL uses back-ticks (as shown above) for delimited identifiers by default.

See "Do different databases use different name quote" for more details.

臻嫒无言 2024-07-23 02:33:07

我并不是说这是一个好主意,但从技术上来说 - 技术上 - 如果您在整个代码中用反引号将这些标识符括起来,MySQL 将允许您创建包含空格的表和列。 例如:

mysql> create table `This is a Table` (`First Name` varchar(50),
    `Name of First Pet` varchar(20));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into `This is a Table` values ('Tom', 'Skippy');
Query OK, 1 row affected (0.01 sec)

mysql> select * from `This is a Table`;
+------------+-------------------+
| First Name | Name of First Pet |
+------------+-------------------+
| Tom        | Skippy            |
+------------+-------------------+
1 row in set (0.00 sec)

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:

mysql> create table `This is a Table` (`First Name` varchar(50),
    `Name of First Pet` varchar(20));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into `This is a Table` values ('Tom', 'Skippy');
Query OK, 1 row affected (0.01 sec)

mysql> select * from `This is a Table`;
+------------+-------------------+
| First Name | Name of First Pet |
+------------+-------------------+
| Tom        | Skippy            |
+------------+-------------------+
1 row in set (0.00 sec)
何以心动 2024-07-23 02:33:07

听说过列别名吗?

选择
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';

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文