I have 3 tables as follow:
comments
|id| |uid| |tid|
tracks
|id| |uid|
notifications
|id| |from| |tox|
How can I UPDATE notifications and SET tox as equal to the tracks.uid of its relative tracks.id that is equal to the last comments.tid value?
I tried this without success:
UPDATE notifications SET tox = (
SELECT uid FROM tracks
INNER JOIN comments ON tracks.id = comments.tid ORDER BY comments.tid DESC LIMIT 1
WHERE comments.tid=tracks.id)
WHERE tox = 0 ORDER BY id DESC LIMIT 1;
UPDATE
First I edit and moved the ORDER BY at the end as suggested. After that I got a different error 1052 - Column 'typeid' in field list is ambiguous.
I solved it like:
UPDATE `notifications` SET `tox` = (
SELECT tracks.uid FROM `tracks`
INNER JOIN `comments` ON tracks.id = comments.tid
ORDER BY comments.id DESC LIMIT 1)
WHERE `tox` = 0 ORDER BY `id` DESC LIMIT 1;
commentswhere thetidvalue is = to thetracks.idrelated to thetracks.uidthat is what I need.tracks.idis a primary key ascomments.idandnotifications.id