Question

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
Was it helpful?

Solution

What about:

INSERT INTO songs(ID)
SELECT DISTINCT songsID FROM iTunesImport
WHERE (songsID IS NOT NULL)
AND songsID NOT IN (SELECT ID FROM songs)

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top