-1

I have two tables profieltest and parametertest.

I want the value column in profieltest to be updated with the parameters from parametertest where the user id is the same and where the field_id = 2 in profieltest . However if the combination between user and field_id in profieltest doesn't exist i want it to be inserted from parametertest. i tried it with an on duplicate key statement with an unique key: CREATE UNIQUE INDEX uniqfield ON profieltest (field_id, user), also profieltest.id is an auto increment value. I tried the following query but it gives me an error :

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't1(t1.user, t1.field_id, t1.value) VALUES( id ,t1.user = t2.user, t1.field_' at line 1

INSERT INTO profieltest t1(t1.user, t1.field_id, t1.value)
VALUES( id ,t1.user = t2.user, t1.field_id=2,  t1.value=t2.parameter)
  SELECT t2.user, t2.parameter
  FROM parametertest t2
  ON DUPLICATE KEY
  UPDATE  SET t1.value = t2.parameter    
  WHERE t1.User = t2.user
    AND t1.Field_id = 2

edit: added the error.

Florian
  • 725
  • 6
  • 27
  • sorry, the error is: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't1 (t1.user, t1.field_id, t1.value) Values( , t2.user, 2, t2.parameter) ' at line 1 – Florian May 28 '14 at 11:06

1 Answers1

0

The following query worked for me:

INSERT INTO Profieltest ( field_id,  user, value )

SELECT field_id, User,  value
FROM parametertest
    WHERE   
    (field_id = 2
    OR field_id  =  6 
    OR field_id = 7)

On duplicate KEY UPDATE 
profieltest.value=(SELECT t2.value
  FROM parametertest t2
  WHERE t2.field_id =profieltest.field_id AND  t2.user=profieltest.user )
Florian
  • 725
  • 6
  • 27