2012-01-11 70 views
0

我想[更新]從交易表TRANSACTION_ID到CMAS表,但我失去了一些東西......不知道從哪裏引用CMAS表,所以它不會出錯#1054 - Unknown column 'cmas.property_id' in 'where clause'MySQL的桌到桌SQL

INSERT INTO `cmas` (`transaction_id`) 
SELECT `transaction_id` 
FROM `transactions` WHERE transactions.property_id = cmas.property_id // this bit is wrong! 

回答

2

既然你提到的評論的更新,這可能是你在找什麼:

UPDATE cmas, transactions 
SET cmas.transaction_id = transactions.transaction_id 
WHERE transactions.property_id = cmas.property_id 
+0

這樣做!謝謝。 – Robert 2012-01-11 01:45:46

1

你應該把它改寫這樣的:

INSERT INTO `cmas` (`transaction_id`) 
SELECT `transaction_id` 
FROM `transactions` WHERE transactions.property_id IN (select property_id from `cmas`) 

問題是存在於左部沒有選擇,所以表達「transactions.property_id = cmas.property_id」是meaninless,因爲沒有CMAS選擇完成。

+0

這個運行,但犯規更新CMAS TRANSACTION_ID – Robert 2012-01-11 01:25:47

1
INSERT INTO cmas (transaction_id) 
SELECT transaction_id 
FROM transactions,cmas 
WHERE transactions.property_id = cmas.property_id 
+0

此運行(當我修改SELECT來'交易.transaction_id')但不更新cmas transaction_id – Robert 2012-01-11 01:25:14

+0

,因爲這是比我的更好的解決方案,我會在這裏評論: – kenota 2012-01-11 01:27:25

+0

它呢,它返回transaction_id – Robert 2012-01-11 01:33:12