mysql - 在mysql case语句中使用类似模式与两个不同的表

标签 mysql sql-like mysql-error-1064 procedures routines

我有一个包含名称的表 (names)。我有另一个保存 ids (user)。 我有一个条件构造 - 一个 case-when 语句,如果名称表中的名称符合特定条件,则该语句应该将 id 插入到用户表中。

我使用了 like % 来匹配字符串模式:

delimiter //
create procedure name_matching (in names.name varchar, out id int) 
begin  
  case 
    when names.name like 's%_%a' then
      insert into user (id) values ('1'); 
    else
      insert into user (id) values ('2'); 
  end case  
end//

这会在 mysql 终端上输出错误 1064。

有没有办法以不同的方式做到这一点?

最佳答案

您的程序存在一些小问题。你需要在 end case 之后有一个分号,你需要为输入参数列表中的 varchar 指定字段大小,等等。以下内容适用于 MySQL 5.1( “works”含义:创建过程时不会产生错误):

delimiter $$
create procedure name_matching (in name varchar(500)) 
begin
  case
    when name like 's%_%a' then
      insert into user (id) values (1); 
    else
      insert into user (id) values (2); 
  end case;
end $$

关于mysql - 在mysql case语句中使用类似模式与两个不同的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5469236/

相关文章:

mysql - 在 SQL 中选择多个日期

sql - MySQL计算一个复杂的表达式

mysql - 使用 sails 作为 REST API 的 Sails passport js 集成

arrays - 查找文本数组包含与输入相似的值的行

MySQL:我无法执行脚本

Mysql 错误号 : 150 Cannot create Table

mysql - 在 MySQL 中选择表的键列表

mysql - 更快 LIKE '%XXX'

PHP 运行 MySQL 查询

mysql - 向数据库插入数据时出现重复条目​​错误