Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-05
6
Medium Priority
?
677 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
What we learned in Webroot's webinar on multi-vector protection.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

722 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