将MySQL列别名设置为计算日期 [英] Set MySQL Column Alias to a Calculated Date

查看:519
本文介绍了将MySQL列别名设置为计算日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做这样的事情:

select data AS curdate() from table;

所以结果表如下:

2013-04-26
  data 1
  data 2
  data 3

我无法弄清楚语法,但是必须可行吗?

I can't figure out the syntax, but it must be possible?

我尝试过不带任何引号的情况,这会返回错误.单引号和反引号都将SQL本身作为列标题返回.

I've tried it without quotes of any kind, which returns an error. Single quotes and back ticks both return the SQL itself as the column header.

推荐答案

这是一个不寻常的要求,但是如果您坚持要使用动态sql.

That's an unusual requirement, but if you insist, you'd have to use dynamic sql.

SET @curdate = CURDATE();
SET @sql = CONCAT('SELECT whatever AS "', @curdate, '" FROM whatever');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

这篇关于将MySQL列别名设置为计算日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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