MySQL 過程跳過最新記錄
P粉550323338
P粉550323338 2024-04-04 00:07:19
0
1
371

我有以下三個表;

hm_deliverylog
+------------+----------------------------------+------------------------------------------------------------------------------------+---------------------+-----------------+--------------------------+-------------------+----------+---------+
| deliveryid | deliveryfrom                     | deliveryfilename                                                                   | deliverytime        | deliverysubject | deliverybody             | deliveryipaddress | subjnotr | islendi |
+------------+----------------------------------+------------------------------------------------------------------------------------+---------------------+-----------------+--------------------------+-------------------+----------+---------+
|          1 | [email protected]              | C:\Program Files (x86)\hMailServer\Data\{BACBEB1F-3852-4CD0-A963-337CA956879E}.eml | 2022-04-11 12:47:13 | Hmail Test      | TEST MESAJI PLAIN-TEXT
 | NULL              | NULL     |       0 |
+------------+----------------------------------+------------------------------------------------------------------------------------+---------------------+-----------------+--------------------------+-------------------+----------+---------+
1 row in set (0.002 sec)

m_deliverylog_recipients
+---------------------+------------+--------------------------+
| deliveryrecipientid | deliveryid | deliveryrecipientaddress |
+---------------------+------------+--------------------------+
|                   1 |          1 | [email protected]         |
+---------------------+------------+--------------------------+
mlog
+-------+-------+---------------------+-----------+------------------+------------+---------+---------------------+-----------+------------+
| pr_id | delid | gonderen            | gonip     | alici            | konu       | mesaj   | gontar              | olusturma | guncelleme |
+-------+-------+---------------------+-----------+------------------+------------+---------+---------------------+-----------+------------+
|     1 |     1 | [email protected] | 10.0.0.83 | [email protected] | Hmail Test | Testing | 2022-05-13 09:37:16 | NULL      | NULL       |
+-------+-------+---------------------+-----------+------------------+------------+---------+---------------------+-----------+------------+
1 row in set (0.000 sec)

我正在嘗試使用以下程序將兩個表的資料合併到另一個表;

CREATE DEFINER=`root`@`localhost` PROCEDURE `mlog_birlestir`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
INSERT INTO mlog (delid,gonderen,gonip,alici,konu,mesaj,gontar)
SELECT hm_deliverylog.deliveryid, hm_deliverylog.deliveryfrom, hm_deliverylog.deliveryipaddress, hm_deliverylog_recipients.deliveryrecipientaddress, hm_deliverylog.deliverysubject, hm_deliverylog.deliverybody, hm_deliverylog.deliverytime
from hm_deliverylog,hm_deliverylog_recipients WHERE hm_deliverylog.deliveryid=hm_deliverylog_recipients.deliveryid;
END

在 hm_deliverylog 的插入事件之前建立觸發器並呼叫過程如下;

call mlog_birlestir()

透過上述步驟,當在 hm_deliverylog 和 hmdeliverylog_recipients 上建立記錄時,它應該合併資料並在 mlog 上建立新記錄。但它總是跳過第一筆記錄並跟隨延遲1筆記錄(抱歉我的英文不足以解釋這種情況),讓我解釋如下;

1-刪除了3個表格上的所有記錄

2- 將第一筆記錄插入 hm_deliverylog 和 hm_deliverylog_recipients

3- mlog 上期望的記錄:未建立

4- 在 hm_deliverylog 和 hm_deliverylog_recipients 上建立第二筆記錄

5- mlog 上預期有兩筆記錄:mlog 上已建立第一筆記錄,但沒有第二筆記錄

6- 在 hm_deliverylog 和 hm_deliverylog_recipients 上建立了第三個記錄

7-mlog 上預期有 3 筆記錄:mlog 上已建立第二筆記錄,但沒有第三筆記錄

事情是這樣的,它總是在 mlog 上的最新記錄之前建立一條並跳過最新記錄。

我嘗試將觸發器的事件更改為 AFTER INSERT 但沒有解決問題。

在程式處理的前兩個表上建立記錄,但我無法控制它,我正在尋找解決此問題的解決方法。

我在這裡缺少什麼?

P粉550323338
P粉550323338

全部回覆(1)
P粉294954447

感謝@Akina,我解決了這個問題。

我的程式碼沒問題,但我犯了一個邏輯錯誤,我正在創建調用hm_deliverylog (第一個表)上的過程的觸發器,我應該做的是在hm_deliverylog_recipients (第二個表)上創建觸發器,因為應用程式插入main首先將資料插入第一個表,然後將收件人資料插入第二個表,因此當我將觸發器設為第一個表時,它會運行但檢查第二個表,但第二個表中還沒有記錄,這就是為什麼它不會在第三個表上建立記錄表。

當我在第二個表上建立觸發器時,它運作得很好。

再次感謝@Akina的提醒

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!