在表中插入记录而不重复记录 [英] Insert Records in a Table without duplicating the records

查看:90
本文介绍了在表中插入记录而不重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个MySQL语句.我想重复执行此SQL,以将结果插入到第三张表中,并且要重复第三张表中的记录.

I have this MySQL Statement. I want to execute this SQL repeatedly to insert the result into a Third table withoud duplicating the Records in Third Table.

SELECT  `Client_ID`, `Client_RFID_Number` 
FROM ciom_master AS a
WHERE  (`Client_Active` ='Y' OR `Client_Active` ='y')
    AND NOT EXISTS (
                    SELECT (`Client_Check_Out`)
                    FROM `cio_master` AS b
                    WHERE a.Client_ID = b.Client_ID 
                        and Client_Check_Out = CURDATE() )
 AND NOT EXISTS (
                 SELECT (`Client_Check_In`)
                 FROM `cio_master` AS c
                 WHERE a.Client_ID = c.Client_ID 
                     and Client_Check_In = CURDATE())

我正在尝试遵循Statement,但是它抛出错误

I am attempting following Statement but it is throwing error

INSERT IGNORE INTO cio_alert (`Client_ID`, `Client_RFID_Number`, `Client_Check_Out`, `Client_Check_In`)

SELECT `ciom_master`.`Client_ID`, `ciom_master`.`Client_RFID_Number`,`cio_master`.`Client_Check_Out`, `cio_master`.`Client_Check_In`  FROM ciom_master 
INNER JOIN `cio_master` ON `ciom_master`.`Client_ID` = `ciom_master`.`Client_ID`
 WHERE  EXISTS 
 (SELECT  `Client_ID`, `Client_RFID_Number` 
    FROM ciom_master AS a
    WHERE (`Client_Active` ='Y' OR `Client_Active` ='y')
        AND NOT EXISTS (
                    SELECT (`Client_Check_Out`)
                    FROM `cio_master` AS b
                    WHERE a.Client_ID = b.Client_ID 
                        and Client_Check_Out = CURDATE() )
        AND NOT EXISTS (
                 SELECT (`Client_Check_In`)
                 FROM `cio_master` AS c
                 WHERE a.Client_ID = c.Client_ID 
                     and Client_Check_In = CURDATE())
        AND (
                SELECT `Client_Check_Out` FROM cio_master 
                WHERE ((`Client_Check_Out` IS NOT NULL AND `Client_Check_In` IS NULL) 
                 OR (`Client_Check_Out` IS NULL AND `Client_Check_In` IS NULL)))
        AND (
                SELECT `Client_Check_In` FROM cio_master 
                WHERE ((`Client_Check_Out` IS NOT NULL AND `Client_Check_In` IS NULL) 
                 OR (`Client_Check_Out` IS NULL AND `Client_Check_In` IS NULL)))         
 )

推荐答案

您没有在最后一条select语句中提及存在或不存在的条件...您只是简单地将and条件放在不存在或不存在或不存在的情况下.尝试将任何有效条件放在那.并且在第一个选择语句y r u中使用相同的表两次并且在没有任何别名的情况下进行联接,请尝试一次检查一次.

You didnt mention exist or not exist condition at the last select statement... u simply put the and condition without any exist or not exist or in or not in. Try to put any of the valid condition over there. And also in the first select statement y r u using same table twice and joining without any alias try to check that one also once.

这篇关于在表中插入记录而不重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆