2013-03-08 69 views
1

我有一個150萬記錄在一個臨時表,我想要插入到主表中的一個內部聯接的MySQL表。問題,而試圖插入一個表到另一個

我的代碼有效,但它停在103,613條記錄。 IT不會繼續。爲什麼會停止?爲什麼它不會一路走到最後?

這是我當前的代碼

INSERT INTO phone_calls(next_attempt, created_on, modified_on, status, call_subject, 
account_number, call_code, last_attempt_on, total_attempts, account_id 

,team_id 
,campaign_id 
,call_code_id 
,result_code 
,result_code_id 
,time_zone_id 
,trigger_on 
,first_attempt_on 
,first_attempt_by 
,last_attempt_by 
,modified_by 
,client_id 
,last_call_id 
,call_notes 
,owner_id 
,industry_id 
) 


SELECT 
CASE WHEN next_attempt IS NULL OR next_attempt = '' THEN STR_TO_DATE(replace(t.next_attempt,'/',','),'%m,%d,%Y %T') END as next_attempt, 
CASE WHEN t.created_on IS NULL OR t.created_on = '' THEN '0000-00-00 00:00:00' ELSE STR_TO_DATE(replace(t.created_on,'/',','),'%m,%d,%Y %T') END as created_on, 
CASE WHEN t.modified_on IS NULL OR t.modified_on = '' THEN '0000-00-00 00:00:00' ELSE STR_TO_DATE(replace(t.modified_on,'/',','),'%m,%d,%Y %T') END AS modified_on, 
CONVERT(CASE WHEN t.status IS NULL OR t.status = '' THEN 0 ELSE t.status END, UNSIGNED INTEGER) AS status, 
LEFT(IFNULL(t.call_subject, ''), 100), 
t.account_number, 
CONVERT(CASE WHEN t.callcode IS NULL OR t.callcode = '' THEN 0 ELSE t.callcode END , UNSIGNED INTEGER) AS callcode, STR_TO_DATE(replace(t.last_attempt_on,'/',','),'%m,%d,%Y %T') as last_attempt_on, 
CONVERT(CASE WHEN t.New_Attempts IS NULL OR t.New_Attempts = '' THEN 0 ELSE t.New_Attempts END , UNSIGNED INTEGER) AS New_Attempts, 
a.account_id 
,0 
,0 
,0 
,0 
,0 
,0 
, '0000-00-00 00:00:00' 
, '0000-00-00 00:00:00' 
,1 
,1 
,1 
,1 
,0 
,'IMPORTED FROM CRM' 
,1 
,1 
FROM tmp_table_for_rdi_cms AS t 
INNER JOIN accounts AS a ON a.account_number = t.account_number LIMIT 9999999999; 
these 2 fields are indexed so it runs fast 
a.account_number 
t.account_number 

現在,我知道我將沒有任何價值,有一個類型,如果無符號整數某些領域但這是沒關係,我會在以後的時間來更新這個。

如何執行此INSERT INTO查詢而不丟失任何記錄?

回答

0

「沒有默認值」的問題在於源表中的新列的空值定義爲S NOT NULL ,它們沒有使用默認值定義。

你有三種選擇來解決這個問題:

  1. 定義目標列的空能(離開了NOT NULL)
  2. 定義目標列具有缺省值,即NOT NULL DEFAULT 'some value'
  3. 提供如果該列是空的值,即SELECT ifnull(source_column, 'some value)

截斷不正確INTEGER值的問題,您是從一個char值選擇,並把它放在一個int列,但有些vyes是空白的,所以你需要處理的是:

select if(source_column = '', 0, source_column) 

數據截斷列問題是否存在,是因爲源值比目標列可容納的更大/更長。爲了解決這個問題,將你的目標列定義爲更大(bigint而不是int)或更長(例如text或varchar(80)而不是varchar(20))