samedi 23 mai 2015

MySQL column UPDATE data subsitution from another table using IF (# 1054 - Unknown column 'Practice2.COL 1' in 'where clause')

I hope your day is going better then mine. I thought I had an easy task this morning to combine two tables in mysql (Distrib 5.5.43, for debian-linux-gnu) based on a common column. Basically the structure is as follows:

Practice1
Hip    Description    Purchaser    Price
1      Bill Smith     Sam Dillon   $300,000
2      Justin Boyle   Sarah Jones  $75,000
3      Kevin Bains    Anne Helan   $120,000
4      Greg Demtri    James Coon   $250,000

Practice2
COL 1    COL 2        COL 3
2        James King   $80,000
4        Bill Smell   $300,000

What I'm trying to do is update Practice1 with values from Practice2. The end result would look like this:

Practice1
Hip    Description    Purchaser    Price
1      Bill Smith     Sam Dillon   $300,000
2      Justin Boyle   James King   $80,000
3      Kevin Bains    Anne Helan   $120,000
4      Greg Demtri    Bill Smell   $300,000

What I've tried (and didn't work) so far has been:

UPDATE Practice1
SET Practice1.Purchaser=(SELECT `COL 2` FROM `Practice2` WHERE Practice1.Hip=`Practice2`.`COL 1`),
    Practice1.Price=(SELECT `COL 3` FROM `Practice2` WHERE Practice1.Hip=`Practice2`.`COL 1`)
ORDER BY Practice1.Hip

What ended up happening was the information I wanted was updated, however the information that wasn't suppose to be touch became NULL! I.E.-

Practice1
Hip    Description    Purchaser    Price
1      Bill Smith     NULL         NULL
2      Justin Boyle   James King   $80,000
3      Kevin Bains    NULL         NULL
4      Greg Demtri    Bill Smell   $300,000

So I tried an "IF" type of statement:

UPDATE Practice1
SET Practice1.Purchaser = if(Practice1.Purchaser=Practice2.`COL 2`, Practice1.Purchaser, Practice2.`COL 2`),
    Practice1.Price = if(Practice1.Price=Practice2.`COL 3`, Practice1.Price, Practice2.`COL 3`)
WHERE Practice1.Hip=Practice2.`COL 1`
ORDER BY Practice1.Hip

which gave me the error "1054 - Unknown column 'Practice2.COL 1' in 'where clause'" I think its something basic that I'm missing (I hope).

Aucun commentaire:

Enregistrer un commentaire