如何在WHERE中使用Alias并使用LIMIT进行操作

时间:2015-05-04 00:03:05

标签: mysql sql

大家好

我有我的疑问,但它没有工作,我知道为什么,但我问你是否可以帮助我,我会解释。 请求不是逻辑,但这不是问题。

SELECT (5+5) AS result FROM house WHERE id = (5+5); [WORK]
SELECT (5+5) AS result FROM house LIMIT (5+5);  [#1064 - You have an error in your SQL syntax]
SELECT (5+5) AS result FROM house WHERE id = result; [#1054 - Unknown column 'result' in 'where clause']

你有什么想法解决这个问题吗?我理解错误,但我问我是否可以采取其他方式。

谢谢。

1 个答案:

答案 0 :(得分:0)

http://sqlfiddle.com/#!9/7a2c7/2

SET @result = 5+5;
SELECT id, @result
FROM house 
WHERE id = @result;

http://sqlfiddle.com/#!9/7a2c7/8

SELECT id, @idx:=(5+5) as result
FROM house 
HAVING id  = result;

http://sqlfiddle.com/#!9/7a2c7/10

SET @limit:=3;
SET @s = CONCAT('SELECT id, @idx:=(5+5) as result
FROM house 
LIMIT ',@limit); 
PREPARE stmt1 FROM @s; 
EXECUTE stmt1;