当我跟着查询
Select
case
when (@nume=Start_Date) then null
when (@nume!=Start_Date) then @num
End,
case
when (@nume=Start_Date and @nume:=End_Date) then null
when (@nume!=Start_Date) then @nume
End,
case
when (@nume!=Start_Date and @nume:=End_Date and @num:=Start_Date) then @nume
End
from P,(Select @num:=(Select Start_Date from P order by Start_Date desc Limit 1),@nume:=(Select End_Date from P order by Start_Date desc Limit 1 )) as A
;在下一个回合中,它将整数1的值分配给@nume,而不是按预期的日期分配。
发布于 2015-10-08 11:41:59
“这是应该的”。嗯,文档明确声明:
作为一般规则,除了SET语句之外,您不应该将值赋值给用户变量并在同一语句中读取该值。例如,要增加一个变量,这是可以的: 设置为a= @a + 1; 对于其他语句(如
SELECT),您可能会得到预期的结果,但这并不一定。在下面的语句中,您可能认为MySQL将首先计算@a,然后再执行赋值: 选择@a,@a:=@a+1,. 但是,涉及用户变量的表达式的计算顺序还没有定义。
您对代码应该做什么的解释与文档有直接的矛盾。我建议你再问一个问题,描述你想要解决的问题,包括样本数据和期望的结果。
https://stackoverflow.com/questions/33014589
复制相似问题