1、update 基礎語法sql
UPDATE 表名稱 SET 列名稱 = 新值 WHERE 列名稱 = 某值
上面是咱們常見的更新表的方式,其實咱們還能夠去另一張表的數據來更新當前的表數據,如如今就有這樣子一個要求,將某一條記錄show_order字段設置爲最高+1。ui
表結構以下:spa
查詢的sql以下:code
UPDATE week_recommand w INNER JOIN (SELECT (IF(MAX(wr.show_order) IS NULL, 0, MAX(wr.show_order))) as show_order from week_recommand wr ) b SET w.show_order = b.show_order + 1 WHERE uid = '111'
這裏是將uid = 111的記錄的show_order設置爲最高+1,其實原理是,錶鏈接後,會產生一個相似於臨時的視圖這麼一個東西 blog
一樣,咱們能夠使用子查詢的方式實現上面的功能class
UPDATE week_recommand w SET w.show_order = (select b.show_order from (SELECT (IF(MAX(wr.show_order) IS NULL, 0, MAX(wr.show_order))) as show_order from week_recommand wr ) b ) + 1 WHERE uid = '111'
致謝:感謝您的閱讀!基礎