简体   繁体   English

mySQL存储过程where子句问题

[英]mySQL stored procedure where clause problem

I've got a mySql stored procedure that looks like this-- 我有一个看起来像这样的mySql存储过程 -

delimiter |
create procedure GetEmployeeById(in ID varchar(45))
begin
  select id,
      firstName,
      lastName,
      phone,
      address1,
      address2,
      city,
      state,
      zip,
      username,
      password,
      emptypeid
  from myschema.tblemployees t
  where
      t.id=ID limit 1;
end |
delimiter;

If i don't have the limit 1 in place, it always returns all of the rows in the table--with each record's id value set to the ID parameter. 如果我没有限制1,它总是返回表中的所有行 - 每个记录的id值设置为ID参数。 Why can't i just use where id=ID, and why does it return all of the records when i do? 为什么我不能只使用id = ID的地方,为什么我这样做会返回所有记录? What are the implications of me using limit 1? 我使用限制1的含义是什么? Why am i programming on a saturday night? 为什么我在星期六晚上编程?

Because, it's comparing t.id with itself, which will always be true. 因为,它将t.id与自身进行比较,这将永远是真实的。 Call your formal parameter something else. 调用你的形式参数别的东西。

Column names in MySQL are not case-sensitive. MySQL中的列名称不区分大小写。 The id column in your query hides the parameter named ID , so your where clause is really using two different expressions to refer to the same column. 查询中的id隐藏了名为ID的参数,因此您的where子句实际上使用两个不同的表达式来引用同一列。 And of course a column's value is always equal to itself, so all the records match. 当然,列的值总是等于它自己,因此所有记录都匹配。 Use a different name for the input parameter. 为输入参数使用不同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM