【问题标题】:Having issues on a validation for email on a table在表格上验证电子邮件时遇到问题
【发布时间】:2018-11-27 21:34:36
【问题描述】:

我想要做的是在触发器上验证某个寄存器的电子邮件。例如,如果客户是第一次注册,它应该创建一个包含名字和姓氏的电子邮件(例如:user.last@email.com),但是如果已经有一个同名的注册器它应该使用与 customer_id 后面的名字和姓氏相同的方式创建电子邮件(例如:user.last2@email.com)

我目前的代码如下:

delimiter $
create trigger before_customer_insert
before insert on customer
for each row
begin
 declare emailVal varchar(45);
 declare checkData int;
 declare checkData2 int;

 set checkData = (select count(first_name) from customer where first_name = new.first_name);
 set checkData2 = (select count(last_name) from customer where last_name = new.last_name);
 if((checkData = 1) and (checkData2 = 1)) then
    set new.email = (concat(new.first_name,'.', new.last_name, '@sakilacustomer.org'));
 else
    set new.email = (concat(new.first_name,'.', new.last_name, new.customer_id, '@sakilacustomer.oeg'));
 end if;

 if(new.kind_customer is null) then
    set new.kind_customer = '1';
 end if;

 set new.active = 1;
 end $

我遇到的问题是,当它是第一次注册时,它会插入电子邮件但带有 0,例如“name.last0@email.com”,如果我尝试插入相同的信息,则会显示相同的电子邮件.我尝试更改 if 语句中的逻辑,但仍然显示相同的问题。

【问题讨论】:

    标签: mysql sql triggers


    【解决方案1】:

    你的逻辑是落后的。你有:

    if((checkData = 1) and (checkData2 = 1)) then
    

    但你想要:

    if((checkData = 0) and (checkData2 = 0)) then
    

    当数据库中没有任何具有这些名称的现有记录时,您希望使用不带客户 ID 的名称。

    请注意,我不确定您对分别查找名字和姓氏的检查是否完全符合您的要求。您不想检查整个地址是否按原样找到吗?

    按照它的写法,如果有人叫 Joe Bloggs 尝试加入,如果系统中有其他人叫 Joe,他将是 joe.bloggs1,即使没有其他 Joe Bloggs。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2018-12-22
      • 2019-06-16
      相关资源
      最近更新 更多