mysql 变量 区别,MySQL:@variable与变量。有什么不同? (第2部分)

Ok, building off of the last question I asked, How does Mysql handle the where statment in the following code:

DELIMITER ;//

DROP PROCEDURE IF EXISTS `test`;//

CREATE PROCEDURE `test`

(

id INT

)

BEGIN

SELECT *

FROM some_table

WHERE id = id;

END;//

What does MySQL do in this case? Does it treat the where clause as

some_table.id = id

or does it treat it like

some_table.id = some_table.id

Right now I am doing something like

WHERE id = @id

because I didn't know that there were session variables in MySQL and it didn't complain and I thought that it was an explicit way of saying "where this column equals this variable".

Some might say "duh.. of course it treats it as column = variable" but I could easily have said where "variable = column." So how does it handle this?

解决方案

MySQL's variable naming schema is a bit weird, when having the first look into it. Generally MySQL differentiates between three types of variables:

system variables (global or session scoped): @@varname

user defined variables (they are session scoped): @varname

local variables in stored programs: varname

So naming conflicts, such as those you mentioned above, only arise within stored programs. Therefore you first should try to avoid these naming conflicts by assigning unambiguous parameter names, e.g. by prefxing the parameters with p such as pId. If MySQL encounters an ambiguity it will interpret the reference as the name of a variable (see here):

[...] Local variable names should not

be the same as column names. If an SQL

statement, such as a SELECT ... INTO

statement, contains a reference to a

column and a declared local variable

with the same name, MySQL currently

interprets the reference as the name

of a variable. [...]

The wording currently somehow gives the impression that this behaviour could change in future versions.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值