从触发器更新表

发布于 2025-01-24 10:23:50 字数 925 浏览 3 评论 0原文

我在数据库中有以下表格:

CREATE TABLE `CRUISE-RES` (
`cruiseid` INT,
`end-day` DATE,
`start-day` DATE
PRIMARY KEY (`cruiseid`)); 

CREATE TABLE `ROOM` (
`cruise-id` INT,
`price` FLOAT,
FOREIGN KEY (`cruise-id`));

CREATE TABLE `PROFIT` (
`cruiseid` INT,
`total` FLOAT);

具有以下示例表插入:

-- cruise table inserts
insert into `CRUISE-ID` (`cruiseid`,`start-day`,`end-day`)
values (1, '2022/01/01', '2022/01/05'), (1, '2022/01/05', '2022/01/10'), (2, '2022/01/05', '2022/01/10')

-- room table inserts
insert into ROOM (price,`cruise-id`)
values (5,1), (10,1), (25,2)

我还具有以下功能,该功能根据每天的Cruise-Res *价格天数,显示每个Cruiseid的利润。

SELECT c.`cruiseid`, sum(rm.`price`*(DATEDIFF(c.`end-date`, c.`start-date`))) AS 'total_profit'
FROM ROOM rm
JOIN `CRUISE-RES` c
ON rm.`cruise-id` = c.cruiseid
GROUP BY rm.`cruise-id`,'cruiseid'

如何在每次插入巡航分区表之后更新利润表的触发器上使用此信息?

I have the following tables in a database:

CREATE TABLE `CRUISE-RES` (
`cruiseid` INT,
`end-day` DATE,
`start-day` DATE
PRIMARY KEY (`cruiseid`)); 

CREATE TABLE `ROOM` (
`cruise-id` INT,
`price` FLOAT,
FOREIGN KEY (`cruise-id`));

CREATE TABLE `PROFIT` (
`cruiseid` INT,
`total` FLOAT);

With the following sample table inserts:

-- cruise table inserts
insert into `CRUISE-ID` (`cruiseid`,`start-day`,`end-day`)
values (1, '2022/01/01', '2022/01/05'), (1, '2022/01/05', '2022/01/10'), (2, '2022/01/05', '2022/01/10')

-- room table inserts
insert into ROOM (price,`cruise-id`)
values (5,1), (10,1), (25,2)

I also have the following function that shows the profit of each cruiseid based on the number of days in the CRUISE-RES * price per day.

SELECT c.`cruiseid`, sum(rm.`price`*(DATEDIFF(c.`end-date`, c.`start-date`))) AS 'total_profit'
FROM ROOM rm
JOIN `CRUISE-RES` c
ON rm.`cruise-id` = c.cruiseid
GROUP BY rm.`cruise-id`,'cruiseid'

How can I use this information on a trigger that updates the PROFIT table after each insert into CRUISE-RES table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文