我想运行像
这样的东西 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'
我见过Advanced MySql Query: Update table with info from another table
但我只想要满足WHERE
子句的最后一个条目,因此DESC
和LIMIT 1
。如何在SET
信息中包含这些条件?
(我的目标是更新个人资料图片到最近的个人资料图片,例如删除或某事后)
答案 0 :(得分:1)
使用带有limit和desc的select查询,并在id简单
上获取id和fire update答案 1 :(得分:1)
update
支持order by
和limit
:
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;