使用MySQL中的AS关键字从表格的另一列中选择一个名称。

huangapple go评论63阅读模式
英文:

use AS in mysql to select a name from another column on the table

问题

我需要从表中的一列中选择一个值,作为mysql中另一列的名称

例如::

从表中选择列 AS(从表中选择列 where id = 1)
from 表;

它给我一个语法错误。如何在AS函数中使用select语句

实际上,我需要使用mysql中的AS函数将一列中的值设置为另一列的名称。

英文:

I need to select a value from a column in the table as a name of the another column in mysql

for ex ::
select column AS (select column from table where id = 1)
from table;

it give me a syntax error .. How can I use select statement inside AS Function

Actually I need to set a value from a column as a name to another column using AS Function in mysql

答案1

得分: 1

答案很简单:在SQL中不可能实现。列别名是常量。

英文:

The answer is simple: It is not possible in SQL. Column aliases are constants.

答案2

得分: 1

您无法在单个查询中执行此操作。在解析查询之前(即在查询开始读取数据之前),所有标识符都必须在查询中固定,包括列名和列别名。列的名称或别名不能根据查询期间读取的数据在运行时设置。

但您可以通过两个查询来实现您想要的操作。

  1. 查询以获取您想要使用的列别名名称。这将返回一个字符串。
  2. 在格式化第二个查询时使用该字符串。然后,该列别名将在准备第二个查询时被固定。
英文:

You can't do this in a single query. All identifiers must be fixed in the query at the time it is parsed, which is before the query begins reading data. This includes column names and column aliases. The names or aliases of columns cannot be set at runtime based on data read during the query.

But you can do what you want in two queries.

  1. Query to get the column alias name you want to use. This returns a string.
  2. Use that string as you format the second query. Then the column alias will be fixed in that second query by the time you prepare it.

huangapple
  • 本文由 发表于 2023年2月8日 21:07:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定