关于My Sql update语句不能用子查询的解决办法
生活随笔
收集整理的這篇文章主要介紹了
关于My Sql update语句不能用子查询的解决办法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在使用My Sql數據庫語法操作update時,第一時間想到的是一下寫法:
UPDATE purchase_request_detail SET convert_to_voucher_id=2, convert_to_voucher_type='inventory-voucher' WHERE detail_id IN (select detail_id from purchase_request_detail where request_id=1 and item_id=1) ;但是這個時候就會報錯:You can't specify target table 'xxx' for update in FROM
?
My Sql的update的一些特點
1、update 時,更新的表不能在set和where中用于子查詢;
2、update 時,可以對多個表進行更新(Sql Server不行);
???????? ?如:update table_a A,table_b B set A.B_ID=B.ID ,B.A_ID=A.ID; ?
3、update 后面可以做任意的查詢,這個作用等同于FROM;
?
所以My Sql update是不允許使用子查詢的,正確寫法是:
UPDATE purchase_request_detail AS table_1 INNER JOIN (select detail_id from purchase_request_detail where request_id=1 and item_id=1) AS table_2 SET convert_to_voucher_id=2, convert_to_voucher_type='inventory-voucher' WHERE table_1.detail_id = table_2.detail_id;?
轉載于:https://www.cnblogs.com/liuchenxing/p/9178665.html
總結
以上是生活随笔為你收集整理的关于My Sql update语句不能用子查询的解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 你知道我们平时在CSS中写的%都是相对于
- 下一篇: ionic3学习之总结