1

Very simple... I have the songs table with existing primary key values, some of which exist already in the songsID column of the iTunesImport table.

The point of the command is to add new values from the iTunesImport table as new entries in the songs table.

I've read a bunch of examples, and they say to use the following syntax, the only difference being that I need to use SELECT DISTINCT, given that there are multiple instances of a given songsID value in the iTunesImport table.

INSERT INTO songs(ID)
SELECT DISTINCT songsID FROM iTunesImport
WHERE (songsID IS NOT NULL)
ON DUPLICATE KEY UPDATE ID = ID

When executing, it returns 0 rows, and the songs table hasn't changed.

BELOW are sections of my tables...

songs

ID, varchar(15), Not NULL   
Name, varchar(200), Not NULL    
SongType, varchar(200), NULL    
Description, varchar(1000), NULL
DebutDate, date, NULL
TimeStamp, timestamp, Not NULL

with following rows

2nd        2nd Self         Original    NULL    NULL    2014-03-11 18:46:40
3xwide     The Triple Wide  Original    NULL    NULL    2014-03-11 18:46:40
40s        40's Theme       Original    NULL    NULL    2014-03-11 18:46:40
ahab       Ahab             Original    NULL    NULL    2014-03-11 18:46:40
allintime  All in Time      Original    NULL    NULL    2014-03-11 18:46:40

My iTunesImport table

iTunesImport

KEY, int(11), No
albumsID, varchar(7), Yes
songsID, varchar(15), Yes
TrackName, varchar(200), No

There are other columns in this table but they are irrelevant in this case

13  p013    stew           "Der Bluten Kat"
14  p013    stew           "Der Bluten Kat"
15  p013    stew           "Ocean Billy"
16  p014    tinkles        Miss Tinkle's Overture
17  p014    wws            Women Wine and Song >
18  p014    utopian        Utopian Fir >
19  p014    katstune       Kat's Tune >
20  p014    utopian        Utopian Fir
21  p014    billy          Ocean Billy >
22  p014    blacksabbath   Black Sabbath >
23  p014    warpigs        War Pigs >
24  p014    billy          Ocean Billy
25  p014    pequod         The Pequod >
26  p014    ahab           Ahab >
27  p014    pequod         The Pequod >
28  p014    andy           Andy's Last Beer
29  p014    mulche         Mulche's Odyssey
30  p015    dumpcity       Dump City
4
  • Can you post a sample of the rows from each of these tables, as well as the SHOW CREATE TABLE output for each (so we can see all the indexing)? If you have incrementing ids in both, and they overlap (or both start from 1, for example) you aren't going to see much difference. May 13, 2014 at 2:07
  • 2
    What happens when you run SELECT DISTINCT i.songsID FROM iTunesImport i WHERE i.songsID IS NOT NULL AND NOT EXISTS (SELECT 1 FROM songs s WHERE s.Id=i.songsId)? If no results, then you don't have any new records to insert into the songs table.
    – sgeddes
    May 13, 2014 at 2:33
  • It seemed to produce all the right results. What are the i's and s's? And I'm having trouble integrating it into my INSERT statement May 13, 2014 at 3:12
  • The query... INSERT INTO songs(ID) SELECT DISTINCT i.songsID FROM iTunesImport i WHERE i.songsID IS NOT NULL AND NOT EXISTS (SELECT 1 FROM songs s WHERE s.Id=i.songsId) ... came up with the error ... Duplicate entry '' for key 'Song Name' .... for which I'm not even trying to affect May 13, 2014 at 3:30

2 Answers 2

1

What about:

INSERT INTO songs(ID)
SELECT DISTINCT songsID FROM iTunesImport
WHERE (songsID IS NOT NULL)
AND songsID NOT IN (SELECT ID FROM songs)
3
  • #1062 - Duplicate entry '' for key 'Song Name' May 13, 2014 at 22:03
  • I simply had to add another column to my INSERT statement, so that the songs.Name entries weren't blank and therefore duplicates... May 13, 2014 at 22:43
  • INSERT INTO songs(ID, Name) SELECT DISTINCT songsID, TrackName FROM iTunesImport WHERE (songsID IS NOT NULL) AND songsID NOT IN (SELECT ID FROM songs) May 13, 2014 at 22:44
-1

I've never seen your WHERE clause before. Are you sure it's correct? I think the following makes better sense.

  INSERT INTO songs(ID)
  SELECT DISTINCT songsID FROM iTunesImport
  WHERE (NOT ISNULL(songsID))
  ON DUPLICATE KEY UPDATE ID = ID
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.