Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL: How do I insert a list of unique values into a second table.

Posted on 2012-04-05
6
Medium Priority
?
684 Views
Last Modified: 2012-04-05
1. This sql code gives me a list of unique ip addresses

SELECT DISTINCT (`ip`) FROM  `log` WHERE `ip` NOT IN (SELECT `ip` FROM ip);

Open in new window


2. This sql code inserts an ip address into a table.

INSERT IGNORE INTO `ip` (`ip`) VALUES ("24.24.24.24");

Open in new window


3. How can I modify this code so that it inserts all of the unique ip addresses from (1) into the ip table?

INSERT IGNORE INTO `ip` (`ip`) VALUES (SELECT DISTINCT (`ip`) FROM  `log` WHERE `ip` NOT IN (SELECT `ip` FROM ip));

Open in new window

0
Comment
Question by:AdrianSmithUK
  • 3
  • 2
6 Comments
 
LVL 21

Expert Comment

by:K V
ID: 37810056
INSERT IGNORE INTO `ip` SELECT DISTINCT (`ip`) FROM  `log` WHERE `ip` NOT IN (SELECT `ip` FROM ip);
0
 

Author Comment

by:AdrianSmithUK
ID: 37810143
hi theGhost_k8

I get this error.

#1136 - Column count doesn't match value count at row 1
0
 

Author Comment

by:AdrianSmithUK
ID: 37810152
I should point out that the ip table has 10+ fields and one of them is called ip.

The log table has 10+ fields and also has a field called ip.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Accepted Solution

by:
K V earned 1600 total points
ID: 37810169
INSERT IGNORE INTO `ip` (`ip`) SELECT DISTINCT (`ip`) FROM  `log` WHERE `ip` NOT IN (SELECT `ip` FROM ip);
0
 

Assisted Solution

by:Shiv_Sg
Shiv_Sg earned 400 total points
ID: 37810176
you would use the keyword VALUES only if you have static values like your query 2. but if the values are coming from another query u need to remove the VALUES keyword. So it ll be

INSERT IGNORE INTO `ip`(`ip`) (SELECT DISTINCT (`ip`) FROM `log` WHERE `ip` NOT IN (SELECT `ip` FROM `ip`))
0
 

Author Closing Comment

by:AdrianSmithUK
ID: 37810178
Many thanks chaps.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

886 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