需要帮助INNER JOIN多个表并使用MySQL中的最后一行值

时间:2015-06-25 12:08:10

标签: mysql inner-join

我有3张表如下:

评论

|id| |uid| |tid|

:跟踪

|id| |uid|

通知

|id| |from| |tox|

UPDATE notificationsSET tox如何等于其相对tracks.uid的{​​{1}}等于上一个tracks.id值?

我尝试了这个没有成功:

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

1 个答案:

答案 0 :(得分: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之后。