2015-07-02 39 views
0

這裏我試圖在表格(調用)中插入一個新行。我試圖得到的唯一值是來自調用者表的caller_id,其餘的都是我已經擁有的字符串。使用INSERT SELECT和VALUES插入到MYSQL數據庫

林不知道我做錯了什麼。如果我沒有弄錯,caller_id是一個外鍵。

     string initialcalls = "INSERT INTO calls (is_outbound, is_active, start_time, dealer_no, caller_id,call_id, is_spam, is_voicemail) SELECT" 
          + "'@is_outbound', '@is_active', '@start_time', '@dealer_no', @caller_id, '@call_id', '@is_spam', '@is_voicemail' FROM callers WHERE callers.id = @fone;"; 
         MySqlCommand initialCall = new MySqlCommand(initialcalls, connection); 
         string dealerNO = Regex.Match(items[10], @"\d+").Value; 



         initialCall.Parameters.AddWithValue("@is_outbound", isOutBound); 
         initialCall.Parameters.AddWithValue("@is_active", is_active); 
         initialCall.Parameters.AddWithValue("@start_time", datetimeSQLstart); 

         initialCall.Parameters.AddWithValue("@dealer_no", dealerNO); 
         initialCall.Parameters.AddWithValue("@caller_id", selectforcallerid); 
         initialCall.Parameters.AddWithValue("@call_id", items[0]); 
         initialCall.Parameters.AddWithValue("@fone", items[8]); 

         initialCall.Parameters.AddWithValue("@is_spam", isSpam); 
         initialCall.Parameters.AddWithValue("@is_voicemail", isVoiceMail); 


         initialCall.ExecuteNonQuery(); 

         this.CloseConnection(); 
+0

發佈錯誤。 –

回答

0

插入和選擇必須在兩個完全獨立的查詢上完成。

INSERT INTO tablename (field1, field2) VALUES (value1, value2); 

SELECT field1, field2 FROM tablename WHERE something > something else; 

您必須引用外部表才能使用外鍵。

SELECT table1.field1, table1.field2 FROM table1, table2 
WHERE table1.id = table2.id 
+0

hmmmm,以及如果我嘗試分配外鍵錯誤,但如果我不分配它,它會得到一個空值。不確定我的主要故障是什麼。 – rem0nster