在表中插入记录而不重复记录

时间:2017-03-21 17:35:35

标签: insert

我有这个MySQL声明。我想重复执行这个SQL,将结果插入到第三个表中,并重复第三个表中的记录。

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但是它正在抛出错误

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)))         
 )

1 个答案:

答案 0 :(得分:0)

你没有在最后一个select语句中提及存在或不存在条件...你只需将条件与条件存在或不存在或不存在或不存在。尝试将任何有效条件放在那里。并且在第一个选择语句中,您使用相同的表两次并且在没有任何别名的情况下连接尝试同时检查一个。