mysql:按数字引用列

发布于 2024-11-30 19:16:23 字数 244 浏览 0 评论 0原文

我想给列别名,但不知道它们的名称,而只知道它们在表中的编号。

像这样的事情:

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

我不知道列的实际名称

(我知道这种需求听起来很奇怪。是的,我可以知道列的名称。这是一个技术问题,如果您知道技术答案,请回答,无论动机如何!)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

白馒头 2024-12-07 19:16:23

您可以做的最接近的是使用 INFORMATION_SCHEMA.COLUMNS 从序数位置查找列名称。我知道这不是您所要求的,但我认为这可能是您能得到的最接近的。例如,您可以构建一个包含第一、第二和第五列的 select 语句,如下所示:

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;

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