Mysql:在查询中使用值作为别名 [英] Mysql: use value as alias in query

查看:33
本文介绍了Mysql:在查询中使用值作为别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一张桌子

create table mymy(A int(2),B int(2))

是否可以使用字段值作为别名?像(不是真的):

is it possible to use a field value as an alias? Something like (not really):

select A as valueOf(B) from mymy.

推荐答案

没有.你不能.在运行查询之前,这些值是未知的.即使可以,在一列中也会有很多可能不同的值.应该使用哪一个?

No. You can't. The values are not known until the query is run. And even if you could, you'd have a lot of possibly different values in one column. Which one should be used?

我能想到提出此类请求的唯一正当理由是,您有某种 EAV 设计并且想要获得 Pivot 结果.

The only valid reason I can imagine for such a request is that you have some kind of EAV design and you want to have a Pivot result.

如果是这种情况,您可以使用 Dymanic SQL(运行查询,获取结果,基于这些结果构建另一个查询并运行该查询.)但这种操作最好在应用程序端完成(获取结果并在那里格式化,根据您的喜好).

If that's the case, you could use Dymanic SQL (run a query, get the results, build another query based on those results and run that one.) But this kind of operations is better done at the application side (get the results and format them there, as you prefer).

这篇关于Mysql:在查询中使用值作为别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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