简体   繁体   English

通过ID从另一个表更新表并选择

[英]Update table from another table by id and select

We have 2 tables 我们有2张桌子

Titles 职称

+------+--------------- ----+---------------------+---------------------+
| 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 |
+------+--------------------+---------------------+---------------------+

Subs 潜艇

+----+----------+----------+---------+---------------------+
| 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. I want to find the last created sub for an id in subs table. 我想在subs表中找到ID的最后一个创建的sub。
  2. Update the subs_updated column by ID in titles table. 通过标题表中的ID更新subs_updated列。

Please some help 请帮忙

try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM