简体   繁体   中英

SQL query causing duplicates

To make this simple, I have only listed the relevant fields below:

Table 1 (question_data_table):
 - UserID
 - QuestionID
 - Strength
 - Timestamp


Table 2 (user_question_waitlist):
 - UserID
 - QuestionID
 - Timestamp

I am running the following query:

INSERT INTO user_question_waitlist (UserID, QuestionID, Timestamp) 
SELECT UserID, QuestionID, Timestamp FROM question_data_table 
WHERE Strength <= 0 AND question_data_table.QuestionID NOT IN 
(SELECT QuestionID FROM user_question_waitlist 
WHERE question_data_table.UserID = user_question_waitlist.UserID)

The issue is, where 5 rows should be inserted into user_question_waitlist. It inserts 31.

在此处输入图片说明

Sample data from question_data_table:

样本数据:

user_question_waitlist data varies a lot, it can be empty for example or contain data for other users.

As an example, using the sample data above if I have the following data in the user_question_waitlist: 在此处输入图片说明

...and then run the query, it inserts 6 rows to produce this: 在此处输入图片说明

尝试将DISTINCT添加到所选内容的末尾。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM