子查询是重用变量的唯一选择吗? [英] Are subqueries the only option to reuse variables?

查看:70
本文介绍了子查询是重用变量的唯一选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以这种形式使用MySQL:

I'd like to use MySQL in this form:

SELECT 1 AS one, one*2 AS two

因为它更短更甜美

SELECT one*2 AS two FROM ( SELECT 1 AS one ) AS sub1

但是前者似乎不起作用,因为它希望其中一个成为一列.

but the former doesn't seem to work because it expects one to be a column.

有没有更简单的方法来实现这种效果而无需子查询?

Is there any easier way to accomplish this effect without subqueries?

不,SELECT 2 AS two不是选项. ;)

推荐答案

select @one := 1 as one, 2 * @one as two;

用户定义的变量

这篇关于子查询是重用变量的唯一选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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