MySQL自动将前缀添加到字段 [英] Mysql auto-add prefixes to fields

查看:464
本文介绍了MySQL自动将前缀添加到字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例表1:

id | field1 | field2
--------------------

表2:

id | id1 | field1 | field2
--------------------------

是否可以这样做:

select t1.*, t2.* from table1 t1 inner join table2 t2 on t1.id = t2.id1

并收到以下结果:

t1_id | t1_fiedl1 | t1_field2 | t2_id | t2_id1 | t2_fiedl1 | t2_field2
----------------------------------------------------------------------

目标是使mysql自动在结果字段中添加前缀,以避免长时间输入,例如

The goal is to make mysql auto add prefixes to resulting fields, to avoid long typings, such as

select t1.id as t1_id, t1.field1 as t1_field1

以此类推

推荐答案

SQL引擎不会为您将查询重写为自动别名"字段-别名必须是明确的.

The SQL engine will not rewrite your query to "auto-alias" fields for you — aliases must be explicit.

但是,客户端代码中有两个选项.

You have two options in client code, however.

首先,您显然可以编写一个抽象,将查询拼凑在一起,并同时提供别名.

First, you could obviously write an abstraction that pieces together the query and provides aliases while doing so.

第二个更简单的方法是使用底层 MYSQL_FIELD 结构.它们包含有关每个字段的字段和表名(以及其他信息),使您可以以编程方式将t1_field1缝合在一起,而无需事先知道字段名.公开这些信息的方式取决于您特定的客户端API.

Second, and easier, is using the information exposed in the underlying MYSQL_FIELD structures associated with each result set. These contain the field and table name (and other information) about each field, allowing you to stitch together t1_field1 programmatically and without knowing field names in advance. How this info is exposed depends on your particular client API.

这篇关于MySQL自动将前缀添加到字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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