Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

Insert Records in a Table without duplicating the records

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

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)))         
 )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...