2017-03-08 65 views
0

我要像更新表到最後的ID從另一個表

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子句中的最後一個條目運行的東西,因此DESCLIMIT 1 。我如何在SET信息中包含這些標準?

(我的目標是更新更改個人資料照片到最新的資料圖片,例如後刪除或某物)限制和desc

+1

移除;限制1後 –

回答

1

使用select查詢,在該ID簡單

獲得ID和消防更新
1

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;