检查记录是否存在于不同 table

Checking if the record exist in different table

我正在开发一个项目,其中 tables c_sent_messages 存储用户发送的消息,c_passwords 存储促销密码,c_received_messages 存储用户发送的消息从系统发送给用户的消息。我想在 c_passwords table 中检查用户发送的密码是否存在。如果存在,我想从 c_passwords table.

中删除记录

下面显示的代码我试过了。对我来说似乎没问题,但我认为我遇到了一些错误和语法问题。

DELIMITER $$
CREATE PROCEDURE flood () BEGIN
    DECLARE
        i INT DEFAULT 0;
    myloop :
    LOOP
        IF
            EXISTS ( SELECT c_passwords.pass FROM c_passwords WHERE c_passwords.pass = c_sent_messages.msg_text ) THEN-- IF c_sent_messages.stats = "N" THEN
            IF
                EXISTS ( SELECT c_sent_messages.stats FROM c_sent_messages WHERE c_sent_messages.stats = "N" ) THEN
                    INSERT INTO c_received_messages ( receiver, sender, msg_text, stats, x_date )
                VALUES
                    (
                        c_sent_messages.sender,
                        "5757",
                        "Code is received successfully.",
                    NOW());
                ELSE INSERT INTO c_received_messages ( receiver, sender, msg_text, stats, x_date )
                VALUES
                    (
                        c_sent_messages.sender,
                        "5757",
                        "Code is used before!",
                    NOW());

            END IF;
            ELSE INSERT INTO c_received_messages ( receiver, sender, msg_text, stats, x_date )
            VALUES
                (
                    c_sent_messages.sender,
                    "5757",
                    "Please send a valid code!",
                NOW());

        END IF;
        DELETE 
        FROM
            c_passwords 
        WHERE
            pass = c_sent_messages.msg_text;

        SET i = i + 1;
        IF
            i = 100 THEN
                LEAVE myloop;

        END IF;

    END LOOP myloop;

END $$DELIMITER;

当我 CALL flood(); 它显示错误 1054 - Unknown column 'c_sent_messages.msg_text' in 'where clause' 我的语法有什么问题?如何解决?或者有人知道其他方法吗?

c_sent_messages:
id  receiver  sender  msg_text stats    x_date
1   5757    5458169867  WCQ67   N   2019-08-05 11:02:21
2   5757    5410323402  DRC73   N   2019-08-05 11:02:21
3   5757    5466491417  IEY72   N   2019-08-05 11:02:21
4   5757    5568790699  ENE72   N   2019-08-05 11:02:21

    c_passwords:
id   pass
1   AAA00
2   AAA10
3   AAA20
4   AAA30
5   AAA40

c_received_messages is now empty with the columns:
id  receiver  sender  msg_text stats    x_date  

我已经尝试根据您的逻辑重新创建该过程。这可以使用简单的语句而不是复杂的语句来完成。

DELIMITER $$
CREATE PROCEDURE flood () BEGIN

-- inserts data into c_received_messages when c_passwords-->pass matches with c_sent_messages -->msg_text and stats is ='N'
INSERT INTO c_received_messages (receiver, sender, msg_text, stats, x_date)
SELECT sm.sender, sm.receiver, sm.msg_text, 'Code is received successfully.', NOW()
FROM c_sent_messages sm
JOIN c_passwords p ON sm.msg_text=p.pass
WHERE sm.stats='N';

-- inserts data into c_received_messages when c_passwords-->pass matches with c_sent_messages -->msg_text and stats is <>'N'(othre than 'N')
INSERT INTO c_received_messages (receiver, sender, msg_text, stats, x_date)
SELECT sm.sender, sm.receiver, sm.msg_text, 'Code is used before!', NOW()
FROM c_sent_messages sm
JOIN c_passwords p ON sm.msg_text=p.pass
WHERE sm.stats <>'N';

-- inserts data into c_received_messages when c_passwords-->pass doesn't matches with c_sent_messages -->msg_text
INSERT INTO c_received_messages (receiver, sender, msg_text, stats, x_date)
SELECT sm.sender, sm.receiver, sm.msg_text, 'Please send a valid code!', NOW()
FROM c_sent_messages sm
LEFT JOIN c_passwords p ON sm.msg_text=p.pass
WHERE p.pass IS NULL;

-- delete the records when c_passwords-->pass matches with c_sent_messages -->msg_text
DELETE p 
FROM c_sent_messages sm
JOIN c_passwords p ON sm.msg_text=p.pass;

END $$
DELIMITER ;