需要帮助 INNER JOIN 多个表并使用 MySQL 中的最后一个值行
Need help to INNER JOIN multiple tables and use last value row in MySQL
我有 3 个表如下:
评论
|id| |uid| |tid|
曲目
|id| |uid|
通知
|id| |from| |tox|
如何让 UPDATE notifications
和 SET tox
等于其相对 tracks.id
的 tracks.uid
等于最后一个 comments.tid
值?
我试过了没有成功:
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;
更新
首先,我按照建议编辑并移动了最后的 ORDER BY
。之后我得到了一个不同的错误 1052 - Column 'typeid' in field list is ambiguous
。
我这样解决的:
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;
我认为你的问题是子查询的语法:
UPDATE notifications
SET tox = (SELECT uid
FROM tracks INNER JOIN
comments
ON tracks.id = comments.tid
WHERE comments.tid=tracks.id
ORDER BY comments.tid DESC
LIMIT 1
)
WHERE tox = 0
ORDER BY id DESC
LIMIT 1;
ORDER BY
总是在 WHERE
之后。
我有 3 个表如下:
评论
|id| |uid| |tid|
曲目
|id| |uid|
通知
|id| |from| |tox|
如何让 UPDATE notifications
和 SET tox
等于其相对 tracks.id
的 tracks.uid
等于最后一个 comments.tid
值?
我试过了没有成功:
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;
更新
首先,我按照建议编辑并移动了最后的 ORDER BY
。之后我得到了一个不同的错误 1052 - Column 'typeid' in field list is ambiguous
。
我这样解决的:
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;
我认为你的问题是子查询的语法:
UPDATE notifications
SET tox = (SELECT uid
FROM tracks INNER JOIN
comments
ON tracks.id = comments.tid
WHERE comments.tid=tracks.id
ORDER BY comments.tid DESC
LIMIT 1
)
WHERE tox = 0
ORDER BY id DESC
LIMIT 1;
ORDER BY
总是在 WHERE
之后。