MySQL:@变量与变量。 有什么不同? (第2部分)

发布于 2024-07-24 15:51:20 字数 691 浏览 7 评论 0 原文

好的,以 我问的最后一个问题为基础,Mysql 是如何工作的处理以下代码中的 where 语句:

DELIMITER ;//
DROP PROCEDURE IF EXISTS `test`;//
CREATE PROCEDURE `test`
(
  id INT
)
BEGIN
  SELECT *
    FROM some_table
   WHERE id = id;
END;//

MySQL 在这种情况下会做什么? 它是否将 where 子句视为

some_table.id = id

或将其视为

some_table.id = some_table.id 

现在我正在做类似的事情,

WHERE id = @id

因为我不知道 MySQL 中存在会话变量,并且它没有抱怨,我认为这是一种明确的表达方式“其中该列等于该变量”。

有些人可能会说“呃..当然它把它当作列=变量”,但我可以很容易地说“变量=列”。 那么它是如何处理这个问题的呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不语却知心 2024-07-31 15:51:20

乍一看,MySQL 的变量命名模式有点奇怪。 一般来说,MySQL 区分三种类型的变量:

因此,命名冲突(例如您上面提到的那些)仅出现在存储程序中。 因此,您首先应该尝试通过分配明确的参数名称来避免这些命名冲突,例如在参数前加上 p(例如 pId)。 如果 MySQL 遇到歧义它会将引用解释为变量的名称 (参见此处):

[...] 局部变量名称不应
与列名相同。 如果一条 SQL
语句,例如 SELECT ... INTO
语句,包含对
列和声明的局部变量
目前同名MySQL
将引用解释为名称
变量的。 [...]

当前的措辞在某种程度上给人一种这种行为可能在未来版本中改变的印象。

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.

滿滿的愛 2024-07-31 15:51:20

我非常确定 @ 表示它是过程中的变量,而不是引用表列。 只有一个名为 id 的表列,因此在本例中它是明确的。 如果您正在进行联接,那么您需要为其添加前缀:

select *
from table1, table2
where table1.id = @id
and table2.some_field = table1.id

无论它在哪里,变量总是需要 @ 前缀,如在此查询中所示:

select id, @id
from table1

第一个 id 是明确的表列 table1.id, @id 引用存储过程变量。

I'm pretty sure that the @ signifies that it's a variable in the procedure, rather than referencing a table column. There is only one table column named id, so in this case it is unambiguous. If you were doing a join, then you would need to prefix it:

select *
from table1, table2
where table1.id = @id
and table2.some_field = table1.id

It doesn't matter where it is, the variable will always require the @ prefix, as in this query:

select id, @id
from table1

the first id is the unambiguous table column table1.id, and the @id references the stored procedure variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文