Solved

MySQL: Duplicate Issue with INSERT INTO... From...

Posted on 2011-09-09
1
261 Views
Last Modified: 2012-05-12
The problem with my query is it adds duplicates.

I do not want an item to be added it it was already added.
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) SELECT '22', `ID` from `Member` WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` = '1' AND `SMSMember` != '1'

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 36511940
you have 2 options:
* use the ON DUPLICATE KEY.. syntax (resp the REPLACE syntax):
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

* or you write your query so it does not return duplicates, resp does not insert rows that are already in the table
INSERT INTO `Group_Members` (`GroupID`,`MemberID`) 
SELECT '22', `ID` from `Member` m
WHERE `UserSuspended` != '1' AND `PremPlusMember` != '1' AND `PremWebMember` = '1' AND `SMSMember` != '1'
 AND NOT EXISTS( select null from Group_Members g WHERE g.GroupID = 22 and g.MemberID = m.ID )

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL database data submission 7 68
Instering to MySQL table 5 48
UPDATE query not working in mysqli php 8 51
MySQL Warning Statements when you have a LIMIT clause. 6 29
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question