我正在尝试执行这样的查询:
SELECT
(17 + 4) AS foo,
foo * 2 AS bar;
它失败了,返回Unknown column 'foo' in 'field list'
。所以我这样做了:
SELECT
(@foo := 17 + 4) AS foo,
@foo * 2 AS bar;
哪个有效。现在的问题是我想在子句中整合这个计算值( foo
) :WHERE
SELECT
(@foo := 17 + 4) AS foo,
@foo * 2 AS bar
FROM lorem
WHERE foo = 21;
失败:Unknown column 'foo' in 'where clause'
。
如果我foo
用@foo
中的替换WHERE
,它可以工作。但是,如果我用一列替换文字,它就会停止工作:
SELECT
(@foo := ipsum) AS foo,
@foo * 2 AS bar
FROM lorem
WHERE @foo = 33;
在 table 中返回包含此内容的空集lorem
:
SELECT * FROM lorem;
+-------+
| ipsum |
+-------+
| 33 |
+-------+