Solved

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

Posted on 2012-04-05
6
666 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:theGhost_k8
Comment Utility
INSERT IGNORE INTO `ip` SELECT DISTINCT (`ip`) FROM  `log` WHERE `ip` NOT IN (SELECT `ip` FROM ip);
0
 

Author Comment

by:AdrianSmithUK
Comment Utility
hi theGhost_k8

I get this error.

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

Author Comment

by:AdrianSmithUK
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 21

Accepted Solution

by:
theGhost_k8 earned 400 total points
Comment Utility
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 100 total points
Comment Utility
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
Comment Utility
Many thanks chaps.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now