简体   繁体   English

将表更新为另一个表的最后一个ID

[英]Update Table to last ID from another table

I want to run something like 我想运行类似

 UPDATE TABLE profile
 SET profile_photo = (SELECT photo_id FROM photos WHERE profile_id 
 = 'someprofileid' ORDER BY photo_id DESC LIMIT 1;)
 WHERE 'somecolumn' = 'some criteria'

I have seen Advanced MySql Query: Update table with info from another table 我看过高级MySQL查询:使用另一个表中的信息更新表

But I only want the last entry which satisfies the WHERE clause, therefore the DESC and LIMIT 1 . 但是我只想要最后一个满足WHERE子句的条目,因此就是DESCLIMIT 1 How do I included these criteria in the SET information? 如何在SET信息中包含这些条件?

(My goal is to update change a profile picture to the most recent profile picture, after eg a delete or sth) (我的目标是将个人资料图片更改为最新的个人资料图片,例如删除或删除某物)

使用带有limit和desc的select查询并获取ID并对该ID进行简单的更新

update supports order by and limit : update支持order bylimit

 UPDATE TABLE profile p
     SET profile_photo = (SELECT photo_id
                          FROM photos ph
                          WHERE ph.profile_id = 'someprofileid'
                          ORDER BY photo_id DESC
                          LIMIT 1
                         )
     WHERE p.'somecolumn' = 'some criteria'
     ORDER BY p.id DESC   -- you need to order by something
     LIMIT 1;

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

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