将文本添加到MySQL列的名称前面

时间:2012-04-16 16:30:00

标签: mysql

假设查询如:

SELECT * FROM tableA;

如何将a_添加到每个列的名称中?例如,如果有一个列“username”,它将在结果中作为“a_username”访问。

编辑 SELECT username AS a_username格式无效,因为我需要继续使用*字段选择。与JOIN中另一个表的返回列存在JOIN和潜在冲突。我将迭代返回的列(foreach)并且只想将来自特定表(其模式可能更改)的列输出到HTML输入字段,其中站点管理员可以直接编辑字段的内容。有问题的SQL查询看起来像SELECT firstTable.*, anotherTable.someField, anotherTable.someOtherField,并且它们存在firstTable中存在someField或someOtherField的可能性。

感谢。

5 个答案:

答案 0 :(得分:10)

您可以使用INFORMATION_SCHEMA.COLUMNS表来表示查询,然后使用动态SQL来执行它。

首先让我们创建一个名为dotancohen的示例数据库和一个名为mytable的表

mysql> drop database if exists dotancohen;
Query OK, 1 row affected (0.03 sec)

mysql> create database dotancohen;
Query OK, 1 row affected (0.00 sec)

mysql> use dotancohen
Database changed
mysql> create table mytable
    -> (
    ->     id int not null auto_increment,
    ->     username varchar(30),
    ->     realname varchar(30),
    ->     primary key (id)
    -> );
Query OK, 0 rows affected (0.06 sec)

mysql> insert into mytable (realname,username) values
    -> ('rolando','odnalor'),('pamela','alemap'),
    -> ('dominique','euqinimod'),('diamond','dnomaid');
Query OK, 4 rows affected (0.05 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from mytable;
+----+-----------+-----------+
| id | username  | realname  |
+----+-----------+-----------+
|  1 | odnalor   | rolando   |
|  2 | alemap    | pamela    |
|  3 | euqinimod | dominique |
|  4 | dnomaid   | diamond   |
+----+-----------+-----------+
4 rows in set (0.00 sec)

mysql>

这是名为INFORMATION_SCHEMA.COLUMNS的元数据表:

mysql> desc INFORMATION_SCHEMA.COLUMNS;
+--------------------------+---------------------+------+-----+---------+-------+
| Field                    | Type                | Null | Key | Default | Extra |
+--------------------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG            | varchar(512)        | NO   |     |         |       |
| TABLE_SCHEMA             | varchar(64)         | NO   |     |         |       |
| TABLE_NAME               | varchar(64)         | NO   |     |         |       |
| COLUMN_NAME              | varchar(64)         | NO   |     |         |       |
| ORDINAL_POSITION         | bigint(21) unsigned | NO   |     | 0       |       |
| COLUMN_DEFAULT           | longtext            | YES  |     | NULL    |       |
| IS_NULLABLE              | varchar(3)          | NO   |     |         |       |
| DATA_TYPE                | varchar(64)         | NO   |     |         |       |
| CHARACTER_MAXIMUM_LENGTH | bigint(21) unsigned | YES  |     | NULL    |       |
| CHARACTER_OCTET_LENGTH   | bigint(21) unsigned | YES  |     | NULL    |       |
| NUMERIC_PRECISION        | bigint(21) unsigned | YES  |     | NULL    |       |
| NUMERIC_SCALE            | bigint(21) unsigned | YES  |     | NULL    |       |
| CHARACTER_SET_NAME       | varchar(32)         | YES  |     | NULL    |       |
| COLLATION_NAME           | varchar(32)         | YES  |     | NULL    |       |
| COLUMN_TYPE              | longtext            | NO   |     | NULL    |       |
| COLUMN_KEY               | varchar(3)          | NO   |     |         |       |
| EXTRA                    | varchar(27)         | NO   |     |         |       |
| PRIVILEGES               | varchar(80)         | NO   |     |         |       |
| COLUMN_COMMENT           | varchar(1024)       | NO   |     |         |       |
+--------------------------+---------------------+------+-----+---------+-------+
19 rows in set (0.02 sec)

mysql>

您需要从此表中获得以下列:

  • TABLE_SCHEMA
  • 表名
  • 列名
  • ORDINAL_POSITION

您要求的是将column_name和column_name作为前缀a_

以下是查询以及如何执行它:

select concat('select ',column_list,' from ',dbtb) into @newsql
from (select group_concat(concat(column_name,' a_',column_name)) column_list,
concat(table_schema,'.',table_name) dbtb from information_schema.columns
where table_schema = 'dotancohen' and table_name = 'mytable'
order by ordinal_position) A;
select @newsql;
prepare stmt from @newsql;
execute stmt;
deallocate prepare stmt;

让我们执行它

mysql> select concat('select ',column_list,' from ',dbtb) into @newsql
    -> from (select group_concat(concat(column_name,' a_',column_name)) column_list,
    -> concat(table_schema,'.',table_name) dbtb from information_schema.columns
    -> where table_schema = 'dotancohen' and table_name = 'mytable'
    -> order by ordinal_position) A;
Query OK, 1 row affected (0.01 sec)

mysql> select @newsql;
+--------------------------------------------------------------------------------+
| @newsql                                                                        |
+--------------------------------------------------------------------------------+
| select id a_id,username a_username,realname a_realname from dotancohen.mytable |
+--------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> prepare stmt from @newsql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> execute stmt;
+------+------------+------------+
| a_id | a_username | a_realname |
+------+------------+------------+
|    1 | odnalor    | rolando    |
|    2 | alemap     | pamela     |
|    3 | euqinimod  | dominique  |
|    4 | dnomaid    | diamond    |
+------+------------+------------+
4 rows in set (0.01 sec)

mysql> deallocate prepare stmt;
Query OK, 0 rows affected (0.00 sec)

mysql>

试一试!!!

您在问题中提到:SELECT用户名AS a_username格式无效,因为我需要继续使用*字段选择。

要实现我的建议,您需要做的就是使用tableA运行查询,如下所示:

select concat('select ',column_list,' from ',dbtb) into @newsql
from (select group_concat(concat(column_name,' a_',column_name)) column_list,
concat(table_schema,'.',table_name) dbtb from information_schema.columns
where table_schema = DATABASE() and table_name = 'tableA'
order by ordinal_position) A;

检索该查询结果时,只需将其用作提交到mysql_query的查询。

答案 1 :(得分:3)

您需要列出列,例如

SELECT username AS a_username FROM tableA;
或者,后端的后处理,例如,更改代码中的数组键

答案 2 :(得分:2)

使用重命名的列创建视图,例如 -

CREATE VIEW a_view AS SELECT username AS a_username FROM table;

然后参考这个观点。

答案 3 :(得分:1)

如前所述,在常规查询中没有标准的方法来对列名称进行批量添加。

但是如果你真的想要实现它,你可以编写一个存储过程来查询information_schema以获取表中的列列表,然后逐个添加前缀。之后,可以将查询连接为字符串PREPAREEXECUTE

这种方法的缺点是您无法加入存储过程的结果。但是,当然,您也可以为您发出的每种查询创建存储过程。然后,可以将任何表的字段前缀作为单独的通用FUNCTION

然而,所有这些东西听起来像是一种矫枉过正。我建议重命名实际的列,以便它们始终带有前缀,或者只是列出所有带有AS别名的结果字段,如Scibuff和Alister建议的那样。

答案 4 :(得分:0)

我不相信它可以自动对所有列进行,但您可以使用AS

列出任意数量的列
SELECT id    AS a_id, 
       name  AS a_name, 
       email AS a_email  /*, etc....*/
FROM tableA;

我只是为了更清晰而插入换行符。