通过id更新来自另一个表的表并选择

时间:2015-03-10 08:48:54

标签: mysql

我们有2张桌子

标题

+------+--------------- ----+---------------------+---------------------+
| id   | title              | subs_updated        | created_at          |
+------+--------------------+---------------------+---------------------+
|  104 | movie 1            | 2014-11-13 05:40:51 | 2014-11-11 12:08:28 |
|  129 | movie 2            |                     | 2014-11-11 12:08:29 |
+------+--------------------+---------------------+---------------------+

替补

+----+----------+----------+---------+---------------------+
| id |  label   | title_id | subs    | created_at          |
+----+----------+----------+---------+---------------------+
| 13 | English  |      104 | English | 2014-11-12 05:05:39 |
| 15 | Italian  |      104 | Italian | 2014-11-12 05:25:00 |
| 16 | Dutch    |      104 | Dutch   | 2014-11-13 05:40:51 |
| 18 | Arabic   |      129 | Arabic  | 2014-11-12 06:05:28 |
| 19 | Arabic   |      129 | Arabic  | 2014-11-12 06:07:23 |
+----+----------+----------+---------+---------------------+
  1. 我想找到子表中id的最后一个创建子。
  2. 按标题表中的ID更新subs_updated列。

  3. 请一些帮助

1 个答案:

答案 0 :(得分:0)

试试这个

   UPDATE 
  `title` 
SET
  subs_updated = 
  (SELECT 
    created_at
  FROM
    `subs`  
  WHERE subs.title_id = title.id ORDER BY created_at DESC LIMIT 1)