MySQL user-defined variable in WHERE clause

前端 未结 4 1272
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 19:20

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 W         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-18 19:47

    From the MySQL manual page on User Defined Variables:

    As a general rule, you should never assign a value to a user variable and read the value within the same statement. You might get the results you expect, but this is not guaranteed.

    So you should separate the assignment from the select statement:

    SET @id = 10;
    SELECT id, location, @id FROM songs WHERE id = @id;
    

提交回复
热议问题