WHERE子句中的MySQL用户定义变量 [英] MySQL user-defined variable in WHERE clause
本文介绍了WHERE子句中的MySQL用户定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有一种方法可以在WHERE
子句中使用用户定义的变量,如本例所示:
I want to know if there is a way to use a user-defined variable in WHERE
clause, as in this example:
SELECT id, location, @id := 10 FROM songs WHERE id = @id
此查询运行没有错误,但没有按预期运行.
This query runs with no errors but doesn't work as expected.
推荐答案
与Mike E.提出的建议相距不远,但有一条声明:
Not far from what Mike E. proposed, but one statement:
SELECT id, location FROM songs, ( SELECT @id := 10 ) AS var WHERE id = @id;
我使用类似的查询来模拟MySQL中的窗口函数.例如. 行采样-只是在同一条语句中使用变量的示例
I used similar queries to emulate window functions in MySQL. E.g. Row sampling - just an example of using variables in the same statement
这篇关于WHERE子句中的MySQL用户定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文