mysql 引用列_mysql:按数字引用列

bd96500e110b49cbb3cd949968f18be7.png

I want to give columns aliases without knowing their names, but only their number in the table.

Something like this:

select firstColumn as myId, secondColumn as myName, thirdColumn as myLastName

where I don't know the actual names of the columns

(I understand that the need sounds strange. And yes I can know the names of the columns. This is a technical question, please answer if you know the technical answer, regardless of the motivation. Thank you!)

解决方案

The closest you could do is use INFORMATION_SCHEMA.COLUMNS to find the column name from the ordinal position. I realize this isn't what you asked for, but I think it may be as close as you can get. For instance, you could build a select statement having the 1st, 2nd and 5th columns as follows:

SELECT CONCAT("SELECT ",

GROUP_CONCAT(column_name SEPARATOR ", "),

" FROM ", table_name)

FROM information_schema.columns

WHERE table_schema = database()

AND table_name = 'my_table'

AND ordinal_position IN (1,2,5)

GROUP BY table_name

ORDER BY ordinal_position;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值