2012-08-16 82 views
2

我試圖得到這個工作,但得到一個錯誤t_write was not locked.表鎖讀寫ALIAS

我更新山坳make從最高值的山坳snsn + 1

LOCK TABLES 1_makes AS t_read READ, 1_makes as t_write WRITE; 

UPDATE t_write 
SET 
    make = 'Pioneer', 
    sn = (SELECT 
      * 
     from 
      (SELECT 
       MAX(sn) 
      FROM 
       t_read 
      WHERE 
       id <> 0) as new_sn) + 1 
WHERE 
    id = 2; 

UNLOCK TABLES; 

回答

3

如何這一點:

LOCK TABLES 1_makes WRITE, 1_makes AS t_read READ; 

UPDATE 1_makes 
SET 
    make = 'Pioneer', 
    sn = (SELECT 
      * 
     from 
      (SELECT 
       MAX(sn) 
      FROM 
       1_makes AS t_read 
      WHERE 
       id <> 0) as new_sn) + 1 
WHERE 
    id = 2; 

UNLOCK TABLES; 
+0

非常感謝。作品一種享受。 – 2012-08-16 15:17:26

+0

如果答案是有幫助的,我可以建議接受它? )首先,它可能爲其他一些人節省一些時間;其次,我們都得到了一些好的提振。 ) – raina77ow 2012-08-16 15:42:50

+0

是否可以「告訴」 MySQL來自動執行此操作上'UPDATE'(每次不使用'trigger')?或者我將不得不在我的PHP查詢中實現這個? – mdthh 2013-01-30 13:53:20