Sql 自动输入我连接的列

Sql auto input the columns that i concated

我有 Fname、Lname 和 Full Name 列。

我想创建一个函数,当我输入 Fname 和 Lname 时,它​​会出现在 Full Name 列中。

CREATE DEFINER=`root`@`localhost` FUNCTION `inser_customer`(Cname VARCHAR(50), CMname VARCHAR(50),CLname VARCHAR(50), edad VARCHAR(50), sex VARCHAR(50), brgay VARCHAR(50), sity VARCHAR(50), Cno VARCHAR(50), gmail VARCHAR(50)) RETURNS VARCHAR(50) CHARSET latin1
BEGIN
SET @full=(SELECT GROUP_CONCAT(Fname,' ', Lname) FROM customer);

IF EXISTS (SELECT * FROM customer WHERE Fname = Cname AND Lname = CLname)THEN
RETURN "Customer Already Exists";
ELSE INSERT INTO customer (`Fname`,`Mname`,`Lname`,`Birthdate`,`Gender`,`Brgy`,`City`,`ContactNo`,`Email`,`Full Name`)VALUES(Cname, CMname, CLname, edad, sex, brgay, sity, Cno, gmail, @full2);
RETURN "OK";
END IF;
    END$$

谢谢。

你需要为此使用 Trigger

See here

A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.

插入新行后

delimiter //
CREATE TRIGGER full_name_after_insert AFTER INSERT ON customer
FOR EACH ROW
SET NEW.`Full Name` = CONCAT(NEW.`Fname`, " ",NEW.`Lname`)
END;//
delimiter ;

我建议您在更新现有行后设置一个触发器,以实现更好的一致性和维护

delimiter //
CREATE TRIGGER full_name_after_update AFTER UPDATE ON customer
FOR EACH ROW
SET NEW.`Full Name` = CONCAT(NEW.`Fname`, " ",NEW.`Lname`)
END;//
delimiter ;

我建议您先尝试类似功能的单元测试,然后根据您的需要进行调整:

CREATE TABLE `test` (
  `firstname` varchar(20) DEFAULT NULL,
  `lastname` varchar(20) DEFAULT NULL,
  `fullname` varchar(40) DEFAULT NULL
);

然后创建函数:

delimiter //

create function insert_test_data (
  mFirstname varchar(20),
  mLastname varchar(20)
)
returns varchar(50)
begin
  set @fullname = trim( concat( trim(mFirstname), ' ', trim(mLastname) ) );

  if exists (
    select 1 from test
    where firstname = mFirstname
          and lastname = mLastname
  ) then
    return 'Customer already exists';
  else
    insert into test (firstname, lastname, fullname) values
    (mFirstname, mLastname, @full);
    return 'OK';
  end if;

end //

delimiter ;

要插入数据:

select insert_test_data ('john', 'smith');
+------------------------------------+
| insert_test_data ('john', 'smith') |
+------------------------------------+
| OK                                 |
+------------------------------------+

select * from test;
+-----------+----------+----------+
| firstname | lastname | fullname |
+-----------+----------+----------+
| john      | smith    | NULL     |
+-----------+----------+----------+

如果客户已经存在,您会看到:

select insert_test_data ('john', 'smith');
+------------------------------------+
| insert_test_data ('john', 'smith') |
+------------------------------------+
| Customer already exists            |
+------------------------------------+

推荐:

我建议您不要添加计算字段。您可以只输入名字和姓氏。当涉及到提取数据时,只需这样做:

select trim( concat( trim(firstname), ' ', trim(lastname) ) )
from test;

因此您可以即时使用这些信息。