gpt4 book ai didi

mysql - 触发值,调用表列并与文本连接

转载 作者:可可西里 更新时间:2023-11-01 08:58:03 24 4
gpt4 key购买 nike

我无法解决 [CREATE TRIGGER VALUE] 需要调用表格内容并与文本连接的问题。预期结果详见下方结果区。

目前代码可以在一次执行中运行,按预期在 account_log 中创建 2 行。

我已经用 [<-- Needs update?] 标记了我怀疑需要更新的行。

CREATE DATABASE IF NOT EXISTS test6;
USE test6;

DROP TABLE IF EXISTS account;
CREATE TABLE account
(
`id` CHAR(4) PRIMARY KEY,
`name` VARCHAR(25),
`price` DECIMAL(4, 2)
);

DROP TABLE IF EXISTS account_log;
CREATE TABLE account_log
(
`log_id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`timestamp` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`description` VARCHAR(60)
);


DELETE FROM account;
INSERT INTO account
VALUES
('1', 'Adam', 10),
('2', 'Eva', 7)
;

DROP PROCEDURE IF EXISTS product_update;

DELIMITER ;;

CREATE PROCEDURE product_update()

BEGIN
UPDATE account
SET
name = "Ad"
WHERE
id = "1";
END
;;

DELIMITER ;

DROP TRIGGER IF EXISTS after_account_update;

CREATE TRIGGER after_account_update
AFTER UPDATE
ON account
FOR EACH ROW
INSERT INTO
account_log (`description`)
VALUES
('Update detected: Productid: [product-1]') -- <-- Needs update?
;

-- ----------------------------
-- Provoke triggers to execute.
-- ----------------------------
CALL product_update();
CALL product_update();

SELECT * FROM account_log;

结果:

目前的结果是这样的:

(描述内容为纯文本)

+--------+---------------------+-----------------------------------------+
| log_id | timestamp | description |
+--------+---------------------+-----------------------------------------+
| 1 | 2019-03-28 18:14:58 | Update detected: Productid: [product-1] |
| 2 | 2019-03-28 18:14:58 | Update detected: Productid: [product-1] |
+--------+---------------------+-----------------------------------------+

我需要这样的结果

(其中表描述中的值 1 和 2 来自表帐户,列 [id]):

+--------+---------------------+-----------------------------------------+
| log_id | timestamp | description |
+--------+---------------------+-----------------------------------------+
| 1 | 2019-03-28 18:14:58 | Update detected: Productid: 1 |
| 2 | 2019-03-28 18:14:58 | Update detected: Productid: 2 |
+--------+---------------------+-----------------------------------------+

最佳答案

它通过使用下面的 TRIGGER VALUES CONCAT 来工作:

CREATE TRIGGER after_account_insert
AFTER INSERT
ON account
FOR EACH ROW
INSERT INTO
account_log (`description`)
VALUES
(
CONCAT("Update detected: Productid: ", "[", NEW.id, "]", ".")
)
;

关于mysql - 触发值,调用表列并与文本连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55403589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com