mysql 批量添加字段前缀_MySQL自动添加前缀到字段

bd96500e110b49cbb3cd949968f18be7.png

Example table1:

id | field1 | field2

--------------------

table2:

id | id1 | field1 | field2

--------------------------

Is it possible to do this:

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

and receive this result:

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

----------------------------------------------------------------------

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

and so on

解决方案

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值