Copy values from one field to another within a subset
I'm trying to create a query but I'm stuck.
Context On my website users have different fields on their profile. One of
these fields is a checkbox for the newsletter. Now we are going to make a
second newsletter. Every user that is subscribed to the newsletter will be
automatically subscribed to the second and have the option to unsubscribe.
The user that are not subscribed for the original newsletter should not
receive the second newsletter either.
Table The fields are stored in a table "Profile_field". This table has 3
columns
fid => field Id (this can be profile name, address, newsletter, ...)
uid => user Id
value
So for every user I need to copy the value of field1 to field2
The query so far
UPDATE profile_values AS copy
SET value =
(SELECT value
FROM ( Select Value
FROM profile_values as original
WHERE fid = 12
) AS temp
)
WHERE fid=37
;
Now this gives me the error:
ERROR 1242 (21000): Subquery returns more than 1 row
I understand why I have this. It's because in my subquery I don't take
into account that a field returns multiple results because of the
different users. In other words I don't take the user into account.
So I tried something like
FROM ( Select Value
FROM profile_values as original
WHERE fid = 12
) AS temp
WHERE uid=copy.uid
But that doesn't work either.
ERROR 1064 (42000): 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 '"temp" WHERE uid=copy.uid )where fid=37' at line 1
So how can I take the user into account in my query?
Warm regards
Stephan Celis
No comments:
Post a Comment