mysql - 在 mysql 中使用多行触发器将两列合并为一列

标签 mysql

以下是我的表架构:

+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| LicenceID    | int(11)      | NO   | PRI | NULL    |       | 
| PassingRTO   | varchar(4)   | NO   |     | NULL    |       | 
| DLNO         | int(15)      | YES  |     | NULL    |       | 
| DateOfIssue  | date         | NO   |     | NULL    |       | 
| DateOfExpiry | date         | NO   |     | NULL    |       | 
| COV          | varchar(6)   | NO   |     | NULL    |       | 
| DateOfBirth  | date         | NO   |     | NULL    |       | 
| BloodGroup   | varchar(3)   | YES  |     | NULL    |       | 
| FullName     | varchar(50)  | NO   |     | NULL    |       | 
| FathersName  | varchar(50)  | YES  |     | NULL    |       | 
| Address      | varchar(150) | NO   |     | NULL    |       | 
| PinCode      | int(6)       | NO   |     | NULL    |       | 
| IssuingAuth  | int(7)       | NO   |     | NULL    |       | 
| IDIA         | int(11)      | YES  |     | NULL    |       | 
| Valid        | tinyint(4)   | NO   |     | NULL    |       | 
+--------------+--------------+------+-----+---------+-------+

我想做的是当我插入一个新行时,我希望我的 DLNO 作为 PassingRTO+LicenceID 和 IDIA 作为 PassingRTO+IssuingAuth。

我尝试了同样的使用 -

create trigger insert_combined 
after insert on LicencesDB 
for each row 
BEGIN 
set new.IDIA = concat(new.PassingRTO, new.IssuingAuth);
set new.DLNO = concat(new.PassingRTO,new.LicenceID); 
END;

但是给了我一个错误——

ERROR 1362 (HY000): Updating of NEW row is not allowed in after trigger ERROR 1193 (HY000): Unknown system variable 'DLNO' ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1

现在我有两个问题,我们可以在触发器中使用多行吗?我们不能在创建表格时合并两列吗?比如 col1 = col2 + col3?

提前致谢!

最佳答案

不要忘记更改 DELIMITER

DELIMITER $$
create trigger insert_combined 
after insert on LicencesDB 
for each row 
BEGIN 
set new.IDIA = concat(new.PassingRTO, new.IssuingAuth);
set new.DLNO = concat(new.PassingRTO,new.LicenceID); 
END $$
DELIMITER ;

关于mysql - 在 mysql 中使用多行触发器将两列合并为一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16143559/

相关文章:

java - 比较两个表中的值

java - 使用Java将图像上传到MySql数据库

php - 这个复杂的SQL语句怎么写

php - CakePHP 3.0 登录每次都返回 false

php - 为什么我的代码只返回 php mysql 中的每秒结果?

MySQL 显示临时表;

python - 在 Python 3 中,使用 mysql.connector,在 insert 中传递 mysql 函数

php - 带有 PHP/MySQL 可视化设计器的 ORM 工具

java - 加入列的 Hibernate 合并问题

mysql - 在 Amazon EC2 中为 MYSQL 数据库配置 Play 应用程序